hello friends and thank you in advance .I have declared two dimensional arraythe question:why tab and *tab give the same output resultCode:int tab[2][2]={1,2,3,4};printf("%p",tab);printf("%p",*tab);Code:0xbfc5523c0xbfc5523c
hello friends and thank you in advance .I have declared two dimensional arraythe question:why tab and *tab give the same output resultCode:int tab[2][2]={1,2,3,4};printf("%p",tab);printf("%p",*tab);Code:0xbfc5523c0xbfc5523c
An array name used without the indices is, by default, a pointer to the first element of that array.
thank' Matticus but tab is pointer to pointer (tab pointer to *tab) and *tab is pointer to first element of tab (pointer to tab[0][0]) that's true?
Question 6.18
It's a very subtle point, but it's one you need to get to grips with.
The address of each 'row' of your 2D array is a "pointer to an array", not a "pointer to a pointer".
Examples
Remember, arrays are contiguous blocks of memory, so whether you print the address of arr, arr[0] or arr[0][0], it's all just a pointer to a single location in memory. Yes, the type of each pointer is different, but the value is the same.Code:int main() { int arr[2][4]; int *pa; int (*pb)[4]; pa = &arr[0][0]; pa = arr[0]; pb = &arr[0]; pb = arr; return 0; }
Again, since the first element of a struct is at the start of the struct, a pointer to the whole struct (p1) or a pointer to the first element (p2) will have different types, but the same value.Code:struct foo { int bar; }; struct foo var; struct foo *p1 = &var; int *p2 = &var.bar;
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
thank you Salem but i don't understand why tab and tab* give the same result (with static two dimensional array)but with dynamic two dimensional array the result is quite different tab it give the address of *tab
[code]#include
#include
main()
{
int i,j;
int array[n][m];
n=6;
m=8;
clrscr();
printf(" Enter i and j : ");
for(i=0;i
Firstly, your example is not a static two dimensional array unless the definition of tab you showed is at file scope (outside a function, or prefixed with the static keyword inside a function).
Second, I wrote a more detailed response, but cannot post it because the forum software reports an error, and demands extra code tags in several places where - if I put them in - would make the entire post virtually unreadable.
This short summary, however, is that you need to understand that pointers and arrays are different things. The only way you would expect output for your (incorrectly named) "static two dimensional array" to be the same as output for a "dynamically allocated array" is if you believed a pointer and an array are exactly the same things. Which they ARE NOT.