base address of a simple array

This is a discussion on base address of a simple array within the C Programming forums, part of the General Programming Boards category; Could any one tell how to get the base address of an array? or better if there is a predefined ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126

    Smile base address of a simple array

    Could any one tell how to get the base address of an array? or better if there is a predefined function that does this work?
    Thanks in advance.

    Here is what i did.
    Code:
    #include <stdio.h>
    
    int main(void){
    	int array[2][3]={1,2,3,4,5,6};
    	int baseAddress = array; //should I write &array or *array ?? please help
    	printf("base address of array is: %ld\n",baseAddress);
    	return 0;
    }
    I have this but i have warnings...
    "warning: initialization makes integer from pointer without a cast"

    the result is always the same :
    Code:
    base address of array is: -1073743324
    Mac OS 10.6 Snow Leopard : Darwin

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    Use the &#37;p format specifier:
    Code:
    #include <stdio.h>
    
    int main(void){
    	int array[2][3]={{1,2,3},{4,5,6}};
    	printf("base address of array is: %p\n", (void*)array);
    	return 0;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    The base address of an array is always the name of the array. Therefore, in your example:
    Code:
    int array[...]
    "array" is the base address. To printf out the address, do this:
    Code:
    printf("the address of 'array' is &#37;p\n", array) ;
    To look at the address of each array element, do this:
    Code:
    #include <stdio.h>
    int main(int argc, char* argv[])
    {
    	int array[5] ; 
    
    	int i ; 
    	for (i = 0 ; i < sizeof(array) / sizeof(int) ; i++) 
    	{ 
    		printf("element %d is at address %p\n", i, &array[i]) ; 
    	}
    	return 0;
    }
    Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126

    Actually i need it for another purposes than printing

    That is why i used a baseAddress variable.
    How can i get the address without using %p ?
    Mac OS 10.6 Snow Leopard : Darwin

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Code:
    printf("the array is at address &#37;x\n", array ) ;
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    To copy the address to another variable...
    Code:
    int * baseAddress = array ;
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Todd Burch View Post
    Code:
    printf("the array is at address %x\n", array ) ;
    That's however not guaranteed to work, since int and pointers are somtimes not the same size (64-bit machines for example have 64-bit pointers, whilst int are only 32-bit). %p is guaranteed to match the size of a pointer in architectures where pointers have a constant size - in architectures where that is not true, you may need modifiers or some other tricks - but fortunately non-constant size pointers are rare these days.

    If you want to use an int to store a pointer, and subject to the pointer actually fitting in there:
    Code:
    int array[] = { ... };
    int x = (int)array;
    Using a cast tells the compiler "Look, I know it's not the right type, but just trust me, it's what I really want to do" - and of course, if that's not what you wanted, then you know who to blame [hint: it's not the compiler].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    How can i get the address without using &#37;p ?
    If you want to print an address, any address, you must use %p and cast the pointer that holds that address to void*. If you want to store that address, then you store it in a pointer, in this case an int*.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126
    thanks all
    Mac OS 10.6 Snow Leopard : Darwin

  10. #10
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by nacho4d View Post
    That is why i used a baseAddress variable.
    How can i get the address without using %p ?
    Just keep it as a pointer. A pointer is the only type which is guaranteed to be able to hold an address. Pointers can be compared, and subtracted from each other, so I question why you want to convert to int in the first place.

    The specific address is irrelevant, unless you are going to print it.

  11. #11
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126

    Smile

    Quote Originally Posted by brewbuck View Post
    The specific address is irrelevant, unless you are going to print it.
    Actually I am working on Image processing program, and I am supposed to pass the base address of the bitmap to a function in order to get image represented in the screen.

    I know it is not necessary an int.

    The function is defined as follows
    Code:
    CGDataProviderRef CGDataProviderCreateWithData(
    	void *info, //please ignore this
    	cons void *data, //bitmap base address
    	size_t size, //number of bytes
    	CGDataProviderReleaseCallBack releaseDate //please ignore this
    );
    and supposing i have my bitmap as
    Code:
    char bitmapRGB[16*3][16*3] = {...};
    I asked myself how could I get the base address of my bitmap?


    by the way this is not the standard C ... is Objective-C for Mac OS but this function has no Objective-C grammar so is like C.
    Mac OS 10.6 Snow Leopard : Darwin

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    I asked myself how could I get the base address of my bitmap?
    The bitmap base address is a const void* data, so you can just pass the array directly and it will be converted into a pointer to its first element. In C, pointers are implicitly cast to a pointer to void.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    There's a little article that elaborate a little on pointers: http://cpwiki.sf.net/A_pointer_on_pointers
    Remember: an address to something in the virtual memory is a pointer. So if you want to take the starting address of the array, what do you need? A pointer!
    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.

  14. #14
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by nacho4d View Post
    Actually I am working on Image processing program, and I am supposed to pass the base address of the bitmap to a function in order to get image represented in the screen.
    In other words, a pointer to the bitmap. The language "base address" was written by somebody who understands what a pointer is -- it doesn't imply that you must somehow locate the "base address."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making a copy of an array by copying its address..
    By transgalactic2 in forum C Programming
    Replies: 12
    Last Post: 04-20-2009, 11:07 AM
  2. Converting an int to an address in an array
    By bommy in forum C Programming
    Replies: 16
    Last Post: 03-19-2009, 03:02 PM
  3. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 05:06 PM
  4. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM

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