473,388 Members | 1,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

2D arrays in pointer fashion issues

momotaro
357 100+
I have a piece of code that is supposed to fill a 2D array and print it out using pointers, the problem in stead I get only a part of it correct!
here is the code:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. #define MAXROW 2
  4. #define MAXCOL 2
  5.  
  6. main()
  7. {
  8.     int i, j, T[MAXROW][MAXCOL];
  9.  
  10. //filling
  11.     for(i = 0; i < MAXROW; i++)
  12.     {
  13.         for(j = 0; j < MAXCOL; j++)
  14.         {
  15.             printf("Please enter your value (%d,%d): \n", i, j);
  16.             scanf("%d", T+i+(j*MAXROW));
  17.         }
  18.     }
  19.  
  20. //prompting
  21.     for(i = 0; i < MAXROW; i++)
  22.     {
  23.         for(j = 0; j < MAXCOL; j++)
  24.             printf("%d\t", T[i][j]);
  25.         printf("\n");
  26.     }
  27.     getch();
  28. }
example:

I fill it with 1 2 3 4 I get: 1 -xxxxxx 3 -xxxxxx

Please help
Oct 18 '09 #1
7 2062
donbock
2,426 Expert 2GB
Refer to Arrays Revealed for general information about 2D arrays.
Oct 18 '09 #2
in your scanf, your T should be T[0] (so that your pointer is correct). To correct your other mistake, draw your array out on a piece of paper from the point of view of how it is stored in memory.
Oct 18 '09 #3
momotaro
357 100+
Jonathans thx for the help but you didn't get my pointer :p I mean my point :d
am using JUST pointers

thx anyway
Oct 18 '09 #4
T[0] is a pointer since this is a 2-D array . If you want to you could substitute &T[0][0] (see the Arrays Revealed that Don had recommended).

I was commenting before that you have the calculation i+j*MAXROW, but that's not how arrays are laid out in memory, where the column is offset into it's particular row.
Oct 18 '09 #5
Banfa
9,065 Expert Mod 8TB
Do you mean that you don't want to do this?

scanf("%d", &T[i][j]);

Which is the obvious solution.

Your problem with

T+i+(j*MAXROW)

is that the arithmetic is wrong.

T[MAXROW][MAXCOL]

is not MAXCOL array of MAXROW arrays but the other way around MAXROW array of MAXCOL arrays.

Put another way if you have an array

char T[2][3];

the memory map of elements in the array looks something like

Expand|Select|Wrap|Line Numbers
  1. -------------------------
  2. |T[0][0]|T[0][1]|T[0][2]|
  3. -------------------------
  4. |T[1][0]|T[1][1]|T[1][2]|
  5. -------------------------
  6.  
but the way you have coded it it would have to be like this

Expand|Select|Wrap|Line Numbers
  1. -----------------
  2. |T[0][0]|T[1][0]|
  3. -----------------
  4. |T[0][1]|T[1][1]|
  5. -----------------
  6. |T[0][2]|T[1][2]|
  7. -----------------
  8.  
to work
Oct 18 '09 #6
hello,

I have slightly modified given code. When I am using T[i] +j to display address, i am getting correct addresses of array T in output.

But when I am using T + i*MAXCOL +j (mathematically this is correct), I am getting incorrect answer. In the output instead of getting 4 bytes increment one by one to base address I am seeing 8 bytes increment.

Can anyone explain why it is acting differently?

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. #define MAXROW 2
  4. #define MAXCOL 2
  5.  
  6. int main()
  7. {
  8.     int i, j, T[MAXROW][MAXCOL];
  9.  
  10.  
  11.  
  12.     for(i = 0; i < MAXROW; i++)
  13.         {
  14.             for(j = 0; j < MAXCOL; j++)
  15.                 printf("%x\t", &T[i][j]);
  16.             printf("\n");
  17.         }
  18.  
  19. //filling
  20.  
  21.     printf("\nUSING T[i]+j\n");
  22.     for(i = 0; i < MAXROW; i++)
  23.     {
  24.         for(j = 0; j < MAXCOL; j++)
  25.         {
  26.             printf("Address of  (%d,%d): ", i, j);
  27.             printf("%x\t", T[i]+j);
  28.         }
  29.     printf("\n");
  30.     }
  31.  
  32.     printf("\nUSING T+i*MAXCOL +j\n");
  33.     for(i = 0; i < MAXROW; i++)
  34.     {
  35.         for(j = 0; j < MAXCOL; j++)
  36.         {
  37.             printf("Address of  (%d,%d): ", i, j);
  38.             printf("%x\t", T+ i*MAXCOL +j);
  39.         }
  40.     printf("\n");
  41.     }
  42. /*
  43. //prompting
  44.     for(i = 0; i < MAXROW; i++)
  45.     {
  46.         for(j = 0; j < MAXCOL; j++)
  47.             printf("%d\t", T[i][j]);
  48.         printf("\n");
  49.     }
  50. //    getch();
  51. */
  52.     return 0;
  53. }
  54.  

OUTPUT

Expand|Select|Wrap|Line Numbers
  1. bfbeb10c    bfbeb110    
  2. bfbeb114    bfbeb118    
  3.  
  4. USING T[i]+j
  5. Address of  (0,0): bfbeb10c    Address of  (0,1): bfbeb110    
  6. Address of  (1,0): bfbeb114    Address of  (1,1): bfbeb118    
  7.  
  8. USING T+i*MAXCOL +j
  9. Address of  (0,0): bfbeb10c    Address of  (0,1): bfbeb114    
  10. Address of  (1,0): bfbeb11c    Address of  (1,1): bfbeb124    
Oct 19 '09 #7
Banfa
9,065 Expert Mod 8TB
vipin sharma to answer your question you need to ask yourself what type is T interpreted as when used as you have done in pointer arithmetic.

Here's a clue, it is not int *.
Oct 19 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type >...
197
by: Steve Kobes | last post by:
Is this legal? Must it print 4? int a = {{1, 2}, {3, 4}}, *b = a; printf("%d\n", *(b + 3)); --Steve
4
by: Richard Hayden | last post by:
Hi, Why does gcc (3.3.2) give me a 'initialization from incompatible pointer type' warning when compiling: int main(int argc, char** argv) { int testa; int** testp = testa; }
7
by: Bob Rock | last post by:
Hello, converting from the managed to the unmanaged world (and viceversa strings) and byte arrays is something I do often and I'd like to identify the most correct and efficient way to do it....
6
by: Broeisi | last post by:
Hello, I wrote the tiny progam below just to understand arrays and strings better. I have 2 questions about arrays and strings in C. 1. Why is it that when you want to assign a string to an...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
3
by: lisp9000 | last post by:
I was wondering the best way to define and loop through a character array. Most lines of the file I am processing are 80 characters long but when an error occurs in the client which created the log...
43
by: emyl | last post by:
Hi all, here's an elementary question. Assume I have declared two variables, char *a, **b; I can then give a value to a like a="hello world";
127
by: sanjay.vasudevan | last post by:
Why are the following declarations invalid in C? int f(); int f(); It would be great if anyone could also explain the design decision for such a language restricton. Regards, Sanjay
152
by: vippstar | last post by:
The subject might be misleading. Regardless, is this code valid: #include <stdio.h> void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); } int main(void) { double array = { {...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.