Connecting Tech Pros Worldwide Help | Site Map

Pointer help

Member
 
Join Date: May 2007
Posts: 119
#1: Aug 14 '09
I am quite new to pointers and multi dimensional arrays.

In my code I imgData[0][0] to point to the value of v while imgData[0][1] points to reg.num.

What am I doing wrong?

Thanks

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. struct Region
  5.   {
  6.        int num;
  7.        int minX;
  8.        int minY;
  9.        int maxX;
  10.        int maxY;
  11.        int pixCount;
  12.   };
  13.  
  14. struct Region my_struct;           /* define the structure */
  15. void show_name(struct Region *p);  /* function prototype */
  16.  
  17. int main(void)
  18. {
  19.     struct Region *st_ptr;         /* a pointer to a structure */
  20.     st_ptr = &my_struct;        /* point the pointer to my_struct */
  21.     int *imgData[9][2]; 
  22.     int v = 255;
  23.     my_struct.num = 10; 
  24.  
  25.     imgData[0][0] = &v;
  26.     imgData[0][1] = &my_struct.num;
  27.  
  28.     printf("Test 1:%d\n",st_ptr->num);
  29.     printf("Test 2:%d\n",my_struct.num);
  30.  
  31.  
  32.     v=2;
  33.     int i=0;
  34.     for(i=0;i<9;i++){
  35.       /** I would expect imgData[0] to be set with the struct and v values. */
  36.       printf("%d - %d|%d\n", i,imgData[i][0],imgData[i][1] );
  37.     }
  38.  
  39.     my_struct.num = 123;     
  40.     my_struct.minX = 63;
  41.     show_name(st_ptr);          /* pass the pointer */
  42.  
  43.  
  44.  
  45.  return 0;
  46. }
  47.  
  48. void show_name(struct Region *p)
  49. {
  50.     printf("\n%d ", p->num);  /* p points to a structure */
  51.     printf("%d ", p->minX);
  52.     printf("%d\n", p->minY);
  53. }
  54.  
  55.  
  56.  
Expert
 
Join Date: Nov 2006
Location: UK
Posts: 1,320
#2: Aug 14 '09

re: Pointer help


in line
Expand|Select|Wrap|Line Numbers
  1.      printf("%d - %d|%d\n", i,imgData[i][0],imgData[i][1] );
  2.  
you are printing the values of the pointers
to print the value of the things pointed too you dereferrence the pointers
Expand|Select|Wrap|Line Numbers
  1.      printf("%d - %d|%d\n", i,*imgData[i][0],*imgData[i][1] );
  2.  
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 828
#3: Aug 14 '09

re: Pointer help


This link might help: Arrays Revealed.
Reply


Similar C / C++ bytes