1)
Explain the difference between:
Array of characters like char c[] = {'A', 'B', 'C'};
AND
c-string like string = "ABC";
2)
We can have two pointers that point to one memory location such as:
int *intPtr1, *intPtr2;
intPtr1 = &x;
intPtr2 = &x;
a) What method we have to make sure only one pointer points to a variable?
b) Give an example.
3)
a) Can a pointer to a constant receive an address of a constant item and/or non-constant item?
b) Explain and give example(s).
Here is the solution to above problem . Please read the explanation for more information
PLEASE GIVE A THUMBS UP!!!
1)
Explain the difference between:
Array of characters like char c[] = {'A', 'B', 'C'};
AND
c-string like string = "ABC";
Answer
A c character array does not contain the '\0' null character at the end hence it cannot be printed on the console like a c-string , where as the c-string "ABC" contains the '\0' null character at the end and can be printed on the console. '\0' character is used in C to tell the compiler when a c-string has ended
2)
We can have two pointers that point to one memory location such as:
int *intPtr1, *intPtr2;
intPtr1 = &x;
intPtr2 = &x;
Answer
Yes, we can have two pointers pointing to the same memory location, The task of a pointer is to store the memory address using the star operator (*) we can access the address stored inside a pointer. Hence it is perfectly alright situation where two pointers are pointing to same memory
3)
a) Can a pointer to a constant receive an address of a constant item and/or non-constant item?
b) Explain and give example(s).
Answer
Yes, A pointer to constant can store address of a non-constant variable as well, below is the code for same
const int x=5;
const int * p = &x;
int y=7;
p=&y;
This works without any issue
Get Answers For Free
Most questions answered within 1 hours.