Thread: base address of a simple array

  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
    28,413
    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;
    }
    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
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    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
    Mainframe assembler programmer by trade. C coder when I can.

  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
    Chappell Hill, Texas
    Posts
    2,332
    Code:
    printf("the array is at address &#37;x\n", array ) ;
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    To copy the address to another variable...
    Code:
    int * baseAddress = array ;
    Mainframe assembler programmer by trade. C coder when I can.

  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
    28,413
    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*.
    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

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

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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
    28,413
    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.
    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

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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, 09:51 AM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM