View Full Version : accessing 2-d array by single pointer
anon10344
09-12-2012, 05:43 PM
Hello Sir,
In order to access two dimensional array by pointer we use double pointer but how to access two dim. array by single pointer?
Regards
Anon10344
basant
09-12-2012, 05:56 PM
Anon10344,
Suppose you have an array as follows.
int a[10][20][30];
Since all the elements are integers, it is certainly possible to access all of them through just an integer pointer say 'int * ptr'.
-- Basant
anon10344
09-12-2012, 07:00 PM
tried to acces multi dim array via double pointer :
#include<stdio.h>
int main()
{
int a[2][3] = {{3,4,5},{7,8,9}};
printf("the ans is %d\n", *(*(a+1)+2));
return 0;
}
the o/p is 9
==> but when trying to access or print 9 via single pointer i wrote this program :
#include<stdio.h>
int main()
{
int a[2][3] = {{3,4,5},{7,8,9}};
printf("the ans is %d\n", *(a+5);
return 0;
}
the ans is -1080443764
basant
09-12-2012, 07:35 PM
Anon10344.
In your code, 'a+5' is not an int *. That is why you are not getting the correct value.
-- Basant
anon10344
09-12-2012, 07:59 PM
Dear Sir,
In the above code, when I was trying to access *(*(a+1)+2)), I was getting the output as '9', even there '*(*(a+1)+2))' was not int *. Could you please explain as to why was I getting the correct output in the former case.
Thanks & Regards,
Anon10344.
Anon10344,
a+5 is an int ** and not an int *. You were de-referencing this twice with the correct offsets and hence were able to retrieve the values.
Thanks,
Anup
anon10344
09-12-2012, 08:33 PM
Dear Sir,
The task is to access the multi dim array by single pointer and i tried to do that by '*(a+5)' but didnt succeed , besides i was able to aceess multi dim array by double pointer. Could you give me a way to think in the rigt direction in order to access multi dim array by the use of single array.
e.g .when i am doing :
#include<stdio.h>
int main()
{
int a[2][3] = {{3,4,5},{7,8,9}};
printf("the ans is %d\n", *(*(a+1)+2));
return 0;
}
the o/p is 9
this is despite the fact that i didnt declared '*(*(a+1)+2)' as int **
similarly i did try to access the multi dim by use of single pointer as below:
#include<stdio.h>
int main()
{
int a[2][3] = {{3,4,5},{7,8,9}};
printf("the ans is %d\n", *(a+5);
return 0;
}
the ans is -1080443764
WHY THIS DIFFERENCE PLEASE EXPLAIN...
REGARDS
ANON 10344
Anon10344,
Please never type in all caps, it is like SHOUTING. In your case, *(a+5) will return a pointer, keep in mind that a is int **. So, if you de-reference it once you get another int * and not an integer. The idea was that you will declare an int *p and then set p = *((a+0) + 0). This will make it point to a[0][0], then you must traverse the array using row and column calculations.
Thanks,
Anup
basant
09-12-2012, 09:10 PM
Anon10344,
In your code, 'a' is int **. Now if you do pointer arithmetic on this such as add a number (e.g. a+5) to get another pointer, you would get the same type which is again int**. If you de-reference such a pointer, you will get int*. That is why your expression *(a+5) gave you some weired value because it is still int*. Now when you de-reference this again, you would get int values. That is why your original expression '*(*(a+1)+2)' gave you correct value.
For better understanding, I would suggest that you should run this program in gdb and see the types of all the above expressions.
-- Basant
anon10344
09-12-2012, 09:22 PM
Dear Sir,
Thank you for the reply but the above mentioned two programs are completely different from each other. They have no connection among them.
I would like to know how to access multi dim array by single pointer.
PS : Apologies for writing in all caps.
Regards
Anon 10344
Anon10344,
Please look at #8 and #9 above, they contain exact responses to your query.
Thanks,
Anup
anon10457
02-03-2013, 01:20 PM
Hi,
For the process_dbs exercise of array data types I've attached the code.
It works upto some extent but not completely.
It is not picking up the values from db2_name database fully.
Kindly refer the screenshot.
Thanks,
Anon10457
Anon10457,
You need to use GDB to set a breakpoint and see what is going on.
Thanks,
Anup