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: - #include <stdio.h>
-
-
#define MAXROW 2
-
#define MAXCOL 2
-
-
main()
-
{
-
int i, j, T[MAXROW][MAXCOL];
-
-
//filling
-
for(i = 0; i < MAXROW; i++)
-
{
-
for(j = 0; j < MAXCOL; j++)
-
{
-
printf("Please enter your value (%d,%d): \n", i, j);
-
scanf("%d", T+i+(j*MAXROW));
-
}
-
}
-
-
//prompting
-
for(i = 0; i < MAXROW; i++)
-
{
-
for(j = 0; j < MAXCOL; j++)
-
printf("%d\t", T[i][j]);
-
printf("\n");
-
}
-
getch();
-
}
example:
I fill it with 1 2 3 4 I get: 1 -xxxxxx 3 -xxxxxx
Please help
7 2062
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.
Jonathans thx for the help but you didn't get my pointer :p I mean my point :d
am using JUST pointers
thx anyway
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.
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 -
-------------------------
-
|T[0][0]|T[0][1]|T[0][2]|
-
-------------------------
-
|T[1][0]|T[1][1]|T[1][2]|
-
-------------------------
-
but the way you have coded it it would have to be like this -
-----------------
-
|T[0][0]|T[1][0]|
-
-----------------
-
|T[0][1]|T[1][1]|
-
-----------------
-
|T[0][2]|T[1][2]|
-
-----------------
-
to work
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? -
#include <stdio.h>
-
-
#define MAXROW 2
-
#define MAXCOL 2
-
-
int main()
-
{
-
int i, j, T[MAXROW][MAXCOL];
-
-
-
-
for(i = 0; i < MAXROW; i++)
-
{
-
for(j = 0; j < MAXCOL; j++)
-
printf("%x\t", &T[i][j]);
-
printf("\n");
-
}
-
-
//filling
-
-
printf("\nUSING T[i]+j\n");
-
for(i = 0; i < MAXROW; i++)
-
{
-
for(j = 0; j < MAXCOL; j++)
-
{
-
printf("Address of (%d,%d): ", i, j);
-
printf("%x\t", T[i]+j);
-
}
-
printf("\n");
-
}
-
-
printf("\nUSING T+i*MAXCOL +j\n");
-
for(i = 0; i < MAXROW; i++)
-
{
-
for(j = 0; j < MAXCOL; j++)
-
{
-
printf("Address of (%d,%d): ", i, j);
-
printf("%x\t", T+ i*MAXCOL +j);
-
}
-
printf("\n");
-
}
-
/*
-
//prompting
-
for(i = 0; i < MAXROW; i++)
-
{
-
for(j = 0; j < MAXCOL; j++)
-
printf("%d\t", T[i][j]);
-
printf("\n");
-
}
-
// getch();
-
*/
-
return 0;
-
}
-
OUTPUT - bfbeb10c bfbeb110
-
bfbeb114 bfbeb118
-
-
USING T[i]+j
-
Address of (0,0): bfbeb10c Address of (0,1): bfbeb110
-
Address of (1,0): bfbeb114 Address of (1,1): bfbeb118
-
-
USING T+i*MAXCOL +j
-
Address of (0,0): bfbeb10c Address of (0,1): bfbeb114
-
Address of (1,0): bfbeb11c Address of (1,1): bfbeb124
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 *.
Sign in to post your reply or Sign up for a free account.
Similar topics
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
>...
|
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
|
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;
}
|
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....
|
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...
|
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:
...
|
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...
|
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";
|
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
|
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 = { {...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |