0

Hi,
Are 2D arrays positioned in memory the same way as normal arrays?
ex.:

char names[2][5] = {"Dean", "Viky"};

If 'names' is memory location 3000, are the letters necessarily stored like this?

3000 = 'D'
3001 = 'e'
3002 = 'a'
3003 = 'n'
3004 = '\0'
3005 = 'V'
3006 = 'i'
3007 = 'k'
3008 = 'y'
3009 = '\0'

thanks.

5
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by deanus
Featured Replies
  • 2
    Salem 5,138   6 Years Ago

    > If 'names' is memory location 3000, are the letters necessarily stored like this? Yes. > And "Dean" and "Viky" is hold as two another array of characters in another place in memory. No. The OP had a true 2D array, so all the data is contiguous in memory. Your … Read More

0

I think it is stored as

names[0]="Dean"// pointer to the starting character of the string "Dean"
names[1] ="Viky"// pointer to the starting character of the string "Viky"

And "Dean" and "Viky" is hold as two another array of characters in another place in memory.

0

Hi,
Are 2D arrays positioned in memory the same way as normal arrays?
ex.:

char names[2][5] = {"Dean", "Viky"};

If 'names' is memory location 3000, are the letters necessarily stored like this?
...

An easy way to find out is to create the array in main() then pass the array to a function like output(names) . Then output the contents of the pointer one character at a time in hex.

void output(char *p)
{
    int i;
    for (i=0; i<10; i++)
    {
        printf("%2d) %02Xh\n", i, *p); // output the character at the ptr
        p++;   // point at the next character
    }
}

Yes, I used printf() for the output because of the C++ iomanip foolishness -- it makes little sense and this was faster.

2

> If 'names' is memory location 3000, are the letters necessarily stored like this?
Yes.

> And "Dean" and "Viky" is hold as two another array of characters in another place in memory.
No.
The OP had a true 2D array, so all the data is contiguous in memory.

Your answer is correct for.
char *names[2] = {"Dean", "Viky"};

0

OK thanks guys...

Now can anybody tell me....
Is there any way literal values can be passed to a function which accepts an object, instead of passing an object?

What I mean is...

Coords screenPosition;
screenPosition.xPos = 40;
screenPosition.yPos = 12;

locateXY(screenPosition);

is there any way I can pass 40 and 12 to locateXY() without having to create an object?

thanks...

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.