1. KEYPOINTS
STRING HANDLING & DYNAMIC
MEMORY ALLOCATION
PREPARED BY
AKILA K
ASSISTANT PROFESSOR
DEPT-CSE
RMKCET
2. STRING DECLARATIONS
char *p = "String";
char p2[] = "String";
char p3[5] = "String";
char arr[7]={‘s',‘t',‘r',‘i',‘n‘,‘g'};
scanf("%s",str); // to read the string
Printf(“%s”,str); // to print the string-
3. String Declaration
What’s the difference between:
char amessage[ ] = "message"
char *pmessage = "message"
Answer:
char amessage[ ] = "message" // single array
Cannot be reassigned to point to another string „
• The array of characters are located on the stack
• can be mutated i.e. you can change the string contents
char *pmessage = "message" // pointer and array
It is pointer that points to first location of the string
„Pointer can be reassigned to point to another string „
Cannot change the string contents (immutable) „The string is placed in a “read only” section
6. Example
int strlen(char str[])
{
int i = 0;
while (str[i] != ‘0’)
{
i++;
}
return i;
}
int strlen(const char* str)
{
int i = 0;
while (*str != ‘0’)
{
i++;
str++;
}
return i;
}
8. const keyword
const int x; // constant int
x = 2; // illegal - can't modify x
const int* pX; // changeable pointer to constant int
*pX = 3; // illegal - can't use pX to modify an int
pX = &someOtherIntVar; // legal - pX can point somewhere else
int* const pY; // constant pointer to changeable int
*pY = 4; // legal - can use pY to modify an int
pY = &someOtherIntVar; // illegal - can't make pY point anywhere else
const int* const pZ; // const pointer to const int
*pZ = 5; // illegal - can't use pZ to modify an int
pZ = &someOtherIntVar; // illegal - can't make pZ point anywhere else
9. Constant pointer - a constant pointer already pointing to an address, cannot
point to a new address.
Pointer to Constant -type of pointer cannot change the value at the address
pointed by it.
char ch, c;
char *ptr = &ch
ptr = &c
char ch = 'c';
char c = 'a';
char *const ptr = &ch; // A constant pointer
ptr = &c; // wrong
char ch = 'c';
char *ptr = &ch
*ptr = 'a';
char ch = 'c';
const char *ptr = &ch; // A constant pointer
*ptr = 'a';// WRONG!!!
12. Function Pointers
int func (int a, int b)
{
printf("n a = %dn“,a);
printf("n b = %dn“,b);
return 0;
}
int main(void)
{
int(*fptr)(int,int); // Function pointer
fptr = func; // Assign address to function pointer func(2,3);
fptr(2,3);
return 0;
}
20. #include <stdio.h>
union unionJob
{
char name[32]; float salary; int workerNo;
} uJob;
struct structJob
{ char name[32]; float salary;
int workerNo;
} sJob;
int main() {
printf("size of union = %d", sizeof(uJob));
printf("nsize of structure = %d", sizeof(sJob));
return 0; }
21. #include <stdio.h>
union job {
char name[32];
float salary;
int workerNo; } job1;
int main() {
printf("Enter name:n");
scanf("%s", &job1.name);
printf("Enter salary: n");
scanf("%f", &job1.salary);
printf("DisplayingnName :%sn", job1.name);
printf("Salary: %.1f", job1.salary); return 0; }
Enter name Hillary
Enter salary 1234.23
Displaying
Name: f%Bary
Salary: 1234.2
You may get different garbage value or empty string for the name.