C/pointers
Expert: Narendra - 8/25/2007
QuestionQUESTION: hello sir,
my question is on pointers . consider the following code
assume array begins from 65472
main()
{
int a[3][4] = {1,2,3,4
4,3,2,1
7,8,9,0
4,7,8,9
};
printf("/n %u %u ",a+1,&a+1)
}
the output is 65480 65496
how it possible for 1st output
since base address is 65472. so next element(column) should be 2-bytes away i.e it should be 65474(as integer array).also explain 2nd output.
thank you
ANSWER: The syntax of this code is wrong and it will not compile.
Actually, you must have printed one more data to understand this - that is the address of a.
Anyway, here is the modified and working code:
#include<stdio.h>
main()
{
int a[3][4] = {1,2,3,4,
4,3,2,1,
7,8,9,0,
};
printf("\n %u %u %u\n",a,a+1,&a+1);
}
And here is the o/p from my computer:
googol:temp> ./a.out
134505952 134505968 134506000
googol:temp>
Here is the explanation of what is happening:
The address of a is: 134505952
When you say a+1, you must remember that, the definition of a is:
int a[3][4] =
So, it has 4 rows and 3 columns. When you increment a by 1, it goes to 2nd row.
1st row has 4 elements and size of each element is 4. So, the total is 16.
So a+1 becomes = 134505952 + 16 = 134505968
Now, when you make &a+1:
For this you need to understand the way pointer arithmetic works.
If the pointer is a char pointer such as:
char *str;
and suppose the address of str is 10.
If you do str++, the new address will be 11. Only 1 is added, since the size of char is 1.
If the pointer is of type int such as:
int *i;
and supposed the address of i is 10.
If you do i++, the new address will be 14. Here 4 is added, since the size of int is 4.
When you do &a+1, this gets converted to pointer arithmetic.
So the value you get will be:
address of a + (sizeof(a) * 1)
a has got 3 rows and size of each row is 16. So the total size is 48.
So the equations becomes:
134505952 + (48 *1) = 134506000
So, you can see that, this is what you are seeing in the o/p.
Hope it is clear now.
---------- FOLLOW-UP ----------
QUESTION: sir,
it is 3 (three) rows and 4 column. so please correct and give me proper answer
AnswerHi,
Were you not happy with the answers I gave for your question?
Looking at the question, it looks very simple and easy.
But, the answer is not easy!
I work in a software company, which develops one of the best Unix-related Operating Systems.
So, we have many people who are well versed with C compilers and also in & out of OS.
I posed this question to few of them and none of them answered. I asked them, after I sent you the answer. I did this to find out, if anybody has got a different answer or a better answer. But, none of them answered.......and note that they are real experts in the field!!
So, the analysis and the answer were tough.
I am wondering, why you didn't like the answer. Or do you have a different opinion? Otherwise, why you will give such low marks for the ratings?
-Narendra
> QUESTION: sir,
> it is 3 (three) rows and 4 column. so please correct and give me proper answer
First of all, you should remember that, C has got only a "Single Dimensional Array".
So, what happens when we declare an array as:
int a[3][4];
Internally C understands this as an array of 3 values and each value has another 4 values inside.
So, you can interpret in whatever terms you want to - row and column is your interpretation or visualization.
But, look at it in terms of initialization. You have initialized it as:
int a[3][4] = {1,2,3,4,
4,3,2,1,
7,8,9,0,
};
The better way to do this is:
int a[3][4] = {{1,2,3,4},
{4,3,2,1},
{7,8,9,0}
};
So, you can observe that, there are 3 elements and each element is a set of 4 elements each.
Now, you can call them 3 rows and 4 columns or 4 rows and 3 columns, whichever is convenient for you. It doesn't matter to your compiler!