I have a question about pointer | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

I have a question about pointer

int *test() { static int x[4]; for (int i=0; i<4; i++) { x[i] = i%2; } return x; } —————————————— is it means that : *test() = x ? the program continue 👇 —————————————— int main() { int *arr = test(); printf(“%d”, *(arr+3));} ——————————————— so the *arr = test() = what? I don’t know really 😣 can someone explain

6th Dec 2019, 1:34 PM
alicia
3 Answers
+ 4
#1; Hello alicia ,🤓 Don't worry, it's not that hard. Let's understand. For understanding this code you need to understand pointers properly. ●A pointer stores address of another variable. ●An array is collection of elements stored in sequential memory. Sequential means each next element will be at next memory block. Not in arbitrary positions. Just in sequence . ●Arrays and pointers are closely related in C. Let's see how. An array name itself is *pointer* to first element of array. That means in order to access subsequent elements of array you can increment pointer value so it points to any other array element. So as array name is pointer to first name in order to return an array from function you just need to return pointer. In `int *test()` , `int * `signifies that function will return a pointer (named x) which is actually array. Next, the value (array) returned by function is assigned to `int *arr` which is also pointer and can be used as array. Cntd...
6th Dec 2019, 2:13 PM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 4
#2; Now next you need to understand pointer arithmetics. In printf we are adding 3 into address hold by pointer. Which makes it point to element 3 integer blocks[by 3*sizeof(int))]ahead in memory. So as currently it's pointing to first element adding 3 makes it point to 4th element (at index 3) Here *(arr+3) is same as arr[3] ●Now let's see why the array is made static in test() function. The lifetime of normal variables is limited to their block scope. So after exiting a function variable is deallocated from memory. Memory is released for other processes. And as you are returning array from function you'll not want that the pointer will point to something that that doesn't exist. Here `static` helps. Static variables have lifetime as long as program. Even if function exits. Variable retains in memory. And this helps our array to help exist even after function is stopped /exited. wish you luck. don't limit you learning to SL alone. check other resources as well. 😄
6th Dec 2019, 2:15 PM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 2
alicia The as in C, functions can return only one value at a time so this function(test) returns the value of first element of x[]. In main() *arr=test() means *arr=x[0] and as x[] is declared as static so are will also point at x[0] *(arr+3) means starring from x[0] increment 3 times and show that value which must be 1 here in this case Hope it clears all your doubts if then then feel free to ask any querry
6th Dec 2019, 1:50 PM
Arsenic
Arsenic - avatar