Why the address of variable array is equal the variable array?

This is a discussion on Why the address of variable array is equal the variable array? within the C Programming forums, part of the General Programming Boards category; Hi, Code: #include <stdio.h> int main() { int vetor[5], *pointer; printf("vetor = %p\n",vetor); printf("&vetor = %p\n",&vetor); // vetor == &vetor! ...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    1

    Why the address of variable array is equal the variable array?

    Hi,

    Code:
    #include <stdio.h>
    
    
    int main()
    {
        int vetor[5], *pointer;
    
    
        printf("vetor = %p\n",vetor);
        printf("&vetor = %p\n",&vetor);
        // vetor == &vetor!
    
        pointer = vetor;
        printf("pointer = %p\n",pointer);
        printf("&pointer = %p\n",&pointer);
        // pointer != &pointer
    
        return 0;
    }
    Why the address of variable array is equal the variable array?

    The name of variable of array is a pointer, but with pointers is different!

    Thanks

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    First of all you need to cast pointers to void* when using variadic functions like printf().
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by claudiu View Post
    First of all you need to cast pointers to void* when using variadic functions like printf().
    First of all, when you use "first of all", there needs to be a second or next thing. Next, this has nothing to do with it being a varadic function, and everything to do with the format specifier he's using. Finally, it's good to finish up with a "finally", or maybe a "last but not least".

    To the OP, read: http://c-faq.com/aryptr/index.html


    Quzah.
    Last edited by quzah; 05-05-2012 at 01:43 PM.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,579
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,384
    If you have an array as follows:

    Code:
    p[5],*pointer;
    
    // The following are the same
    
    p;
    &p[0];
    
    // That is
    
    p == &p[0]
    
    // Therefore,
    
    pointer = p;
    
    // Puts the address of p[0] in the pointer "pointer"
    This is a basic standard of the c- language.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A double variable equals one, it also doesn't equal one.
    By madmax2006 in forum C Programming
    Replies: 9
    Last Post: 02-17-2010, 11:36 PM
  2. Returning the Address of a Local Variable (Array)
    By Jesdisciple in forum C Programming
    Replies: 9
    Last Post: 08-20-2008, 02:03 AM
  3. Replies: 7
    Last Post: 03-17-2008, 04:20 AM
  4. set string array equal variable
    By WaterNut in forum C++ Programming
    Replies: 3
    Last Post: 06-29-2004, 05:02 PM
  5. variable for array ?
    By black in forum C++ Programming
    Replies: 2
    Last Post: 05-21-2004, 06:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21