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

  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
    4,183
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    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-18-2010, 12:36 AM
  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