I cant understand why i get such an output?

  • Thread starter Thread starter transgalactic
  • Start date Start date
  • Tags Tags
    Output
AI Thread Summary
The discussion centers on the confusion regarding pointer arithmetic and array indexing in C programming. The code snippet provided initializes an integer array and a pointer to its second element. The key point is that when using the pointer with array indexing, `array_ptr[1]` accesses the third element of the original array, which is why the output is 89. The misunderstanding arises from the assumption that `array_ptr[1]` should return the address of the second element instead of its value. It is clarified that in C, pointers and arrays are closely related, and indexing a pointer starts from its current position. Thus, `array_ptr[1]` effectively retrieves the value at the next index in the original array, confirming that `array_ptr` points to `array[1]` and `array_ptr[1]` corresponds to `array[2]`. The suggestion to use `*array_ptr[1]` is incorrect, as it does not achieve the intended result.
transgalactic
Messages
1,386
Reaction score
0
i can't understand why i get such an output??

Code:
int array[] = { 45, 67, 89 };    //1st ine
 int *array_ptr = &array[1];   //2nd line
printf("%i\n", array_ptr[1]);   //3rd line

the second line links the pointer *array_ptr with the value of cell "1"
but in the third line it should have print the address of cell "1"
not its value 89

i think that if i want to print cell one we need to change this line into

Code:
printf("%i\n",*array_ptr[1]);
why i get the value of cell "1"
when by my logic i should get the adress of cell "1"

??
 
Technology news on Phys.org


First of all, all arrays in C start at element ZERO.
So, the second line sets the "array_ptr" to the second cell's address of "array".
Then "array_ptr + 1" is the pointer to the third cell of "array".
And finally, array_ptr[1] is the content of the third cell of "array".
Of course, you could write it as "*(array_ptr + 1)" , too.
 
Last edited:


And the suggestion "*array_ptr[1]" it is not what you would like it was...
 


Let's take it one line at a time.

Code:
int array[] = { 45, 67, 89 };    //1st ine
An array of int of 3 elements, with indexes from 0 to 2. So we have array[0] = 45, array[1] = 67, and array[2] = 89.

Code:
int *array_ptr = &array[1];   //2nd line
&array[1] is the same as &( array[1] ). So, we are assigning the address of array[1] to the pointer to int array_ptr. In other word, array_ptr now contain the address of the variable with the value 67.

Code:
printf("%i\n", array_ptr[1]);   //3rd line
First thing to remember is that in C, array and pointer is (almost always) inter-changable in certain context. array_ptr is a pointer, but when used as an array will start "counting" from where it is pointing to at that time. So, since [1] mean the second element of an array, array_ptr[1] mean the 2nd element of the "array" array_ptr, i.e. one past what it's pointing to now. And since it's currently pointing to array[1], that will be array[2], which holds the value 89, which is what's printed.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top