Thread: arraysname is a pointer

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    1

    arraysname is a pointer

    I have understood the concept of pointers and arrays but still one thing is confusing:
    If I have one dimensional array for eg.
    Code:
    int x[]={10,20,30};
    the x stores the address of is first element i.e x=&x[0][0] but how? This shows that x is a variable that also gets some memory space like x[0],x[1] or any other variable, which contains the address. Am I right? but this does not seems to be true. In C language the array name does not get any space in the memory then how can it has a value..I think anything which has the value gets the space in the memory.

    In two dimensional array,
    Code:
    int num[][]={{10,20,30},{40,50,60},{70,80,90}};
    num[0] stores the address of the num[0][0],
    num stores the address of the num[0],
    *num=num[0]
    *(*num)=num[0][0]

    I have not understood these. I think only num[0][0],num[0][1],num[0][2].....gets the memory. How num, num[0],num[1] gets the memory???

    I have gone through some of the topics here but could'nt find them useful.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by carox
    the x stores the address of is first element i.e x=&x[0][0] but how?
    I do not suggest thinking of it like that. Think of x as an array itself, not a pointer. Also, note that the address of the first element of x is &x[0], not &x[0][0].

    Quote Originally Posted by carox
    This shows that x is a variable that also gets some memory space like x[0],x[1] or any other variable, which contains the address. Am I right?
    Yes, there is space allocated for x, but no, that space would not be the space required for a pointer, since x is an array. sizeof(x) would return the size of the entire array.

    Quote Originally Posted by carox
    In two dimensional array,
    Your example is incorrect. It should be:
    Code:
    int num[][3]={{10,20,30},{40,50,60},{70,80,90}};
    Quote Originally Posted by carox
    num[0] stores the address of the num[0][0],
    num stores the address of the num[0],
    Again, do not think of it in that way. num is an array of 3 arrays of 3 ints. Therefore, num[0] is an array of 3 ints.

    Now, the point is that in many contexts, an array is converted to a pointer to its first element. Consequently, if you pass num as an argument to a function, you get a pointer to an array of 3 ints, and this pointer will point to num[0] (at least at the start of the function).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You seem to misunderstand arrays. Arrays are not pointers, and therefore they do not store a memory address.
    Memory is allocated for the entire array.
    However, when you try to use the array name, it decays to a pointer to its first element.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by carox View Post
    I have understood the concept of pointers and arrays but still one thing is confusing:
    If I have one dimensional array for eg.
    Code:
    int x[]={10,20,30};
    the x stores the address of is first element i.e x=&x[0][0] but how? This shows that x is a variable that also gets some memory space like x[0],x[1] or any other variable, which contains the address. Am I right? but this does not seems to be true. In C language the array name does not get any space in the memory then how can it has a value..I think anything which has the value gets the space in the memory.

    In two dimensional array,
    Code:
    int num[][]={{10,20,30},{40,50,60},{70,80,90}};
    num[0] stores the address of the num[0][0],
    num stores the address of the num[0],
    *num=num[0]
    *(*num)=num[0][0]

    I have not understood these. I think only num[0][0],num[0][1],num[0][2].....gets the memory. How num, num[0],num[1] gets the memory???

    I have gone through some of the topics here but could'nt find them useful.
    Code:
    int x[]={10,20,30};
    Few points to remember:
    x is the base adderss of the array i.e x=&x[0], whereas &x is the address where the whole array is stored. If used with sizeof, x gives the total size of the array.
    For a 2D array, think of this way
    x[0]=(*(x+0)+0)=&x[0][0]=address of x[0][0]
    x[1]=(*(x+1)+0)=&x[1][0]=address of x[1][0]
    and so on.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM