Thread: what difference between address and memory location?

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    5

    what difference between address and memory location?

    Code:
    #include <stdio.h>
    #define SIZE 5
    int *odd(void);
    int main()
    {
    int numbers[SIZE];
    int x;
    &numbers = odds();
    for(x=0; x<SIZE; x++)
         printf("Element #%d = %d\n", x, numbers[x]);
    return(0);
    }
    
    int *odds(void)
    {
    int y[SIZE];
    int x;
    for(x=0; x<SIZE; x++)
         y[x] = 2*x+1;
    return(y)
    }
    I know the problem is "&numbers = odds();"

    and using pointer can fix it.

    but I wanna know why?

    the &numbers thing generates an address, and odds() returns a memory location.

    I always think address is the same as memory location.

    what are their differences? thx

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Address and memory location are two words for the same thing.


    The issue isn't that & gives you an "address" and odds() returns a "memory location". You seem to have a misunderstanding of types, pointers and arrays in C, and you are trying to return the address of a local (stack) variable.


    numbers is an array. When you use the identifier numbers all by itself, without [ ] to index it, you get a pointer to the first element, i.e. &numbers[0]. The type of &numbers[0] is "pointer to int" or "int *". &numbers on the other hand is the address of the numbers array. It will return the same address/memory location as &numbers[0], but it will have a different type, namely "pointer to array of int", or "int (*)[]". You can never assign something to the name of the array, only to an individual element of the array, that is
    Code:
    int numbers[SIZE];
    numbers = x;  // this is never valid
    numbers[0] = 42; // this is okay
    &numbers = x;  // this is also never valid

    As for the second issue, you can not return the address of local variables. That means you can not return an array from a function, nor can you return &x or the like (assuming x is a local variable). The reason is, once the function returns, all of the memory it used on the stack is gone, and the contents may be overwritten by something else in your program. Relying on that memory will result in undefined behavior.
    Code:
    int *foo(void)
    {
        int x;
        int *p;
        int array[3];
    
    
        return &x; // this is invalid
        return array; // this is also invalid
    
    
        p = malloc(sizeof(*p));
        return p;  // this is valid, you are returning the address pointed to by p, i.e. the address returned by malloc
                   // that address will still be valid after foo() is done
    }
    A common way to "return" an array from a function legally is to pass it in as a parameter and fill it that way:
    Code:
    void foo(int array[], int array_size)
    {
        array[0] = 42;
        // array_size is so you don't run off the end of your array, C has no way of knowing where the end is, you must keep track
    }
    int main(void)
    {
        int main_array[SIZE];
        foo(main_array, sizeof(main_array));
        // now array[0] will be 42
        return 0;
    }
    Also, reading through the C-FAQ may help: Arrays and Pointers (this is the section pertinent to your issues, but it's worth reading the whole thing when you have time).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2012, 04:28 AM
  2. Replies: 1
    Last Post: 11-07-2010, 11:39 PM
  3. validating memory - minimum address of valid location
    By m37h0d in forum C++ Programming
    Replies: 12
    Last Post: 09-05-2008, 10:50 PM
  4. Memory Location
    By SomC in forum C Programming
    Replies: 6
    Last Post: 05-08-2004, 02:54 AM
  5. memory location
    By xlordt in forum C Programming
    Replies: 3
    Last Post: 10-21-2002, 09:39 AM