I cant understand why i get such an output?

  • Thread starter transgalactic
  • Start date
  • Tags
    Output
In summary, the code is creating an array of 3 elements and assigning a pointer to the second element. Then, when the pointer is used as an array, it will start counting from its current position and access the 2nd element of that array, which is the 3rd element of the original array and holds the value 89. This is why the output is 89 and not the address of the 2nd element as expected.
  • #1
transgalactic
1,395
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
  • #2


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:
  • #3


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


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.
 

FAQ: I cant understand why i get such an output?

1. Why is my output different than what I expected?

There could be several reasons for this. It could be due to an error in your code, incorrect input data, or a misunderstanding of the underlying concepts. It is important to carefully review your code and check your assumptions to identify the source of the discrepancy.

2. How can I troubleshoot my output to determine the cause of the issue?

One approach is to use debugging tools and techniques to step through your code and track the values of variables. This can help you identify where the problem is occurring and what values are causing unexpected output. It can also be helpful to break down your code into smaller chunks to isolate the issue.

3. What can I do if I am still unable to understand my output after troubleshooting?

If you are still having trouble understanding your output, it can be helpful to seek assistance from others. This could include asking a colleague or mentor for help, posting on online forums for programming support, or consulting documentation or other resources for further clarification.

4. Is it possible that my output is correct and I am just interpreting it incorrectly?

Yes, it is possible that your output is correct but you are interpreting it incorrectly. This could be due to a lack of understanding of the underlying concepts or a mistake in your assumptions. Double-checking your code and seeking clarification from others can help ensure that you are correctly interpreting your output.

5. What steps can I take to improve my understanding of my output?

To improve your understanding of your output, it can be helpful to review your code and documentation carefully, seek assistance from others, and practice problem-solving and critical thinking skills. Additionally, reflecting on your thought process and identifying areas for improvement can help you become a more effective programmer and better understand your output in the future.

Similar threads

Replies
2
Views
1K
Replies
29
Views
2K
Replies
25
Views
2K
Replies
3
Views
2K
Replies
22
Views
3K
Back
Top