char pointers and their addresses.

This is a discussion on char pointers and their addresses. within the C Programming forums, part of the General Programming Boards category; Lets say I have two pointers, both to chars: char *ptr1; char *ptr2; Now lets pretend this: ptr1 = malloc(64) ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    3

    char pointers and their addresses.

    Lets say I have two pointers, both to chars:

    char *ptr1;
    char *ptr2;

    Now lets pretend this:

    ptr1 = malloc(64)
    So we allocated 64 bytes, and ptr1 points to the first byte of those 64 allocated.

    Now poof! I just assigned all 64 values unique byte values.

    Heres my question:

    if I say something like:

    Code:
    ptr2 = malloc(1000) // ignore the 1000, im just allocating excess bytes
    *ptr2 = newImage;
    I'm assuming addresses are 32-bit (meaning they can be expressed in 2-bytes)
    But what im really looking for is, I want to store the memory location (two byte wise) of the current location of newImage in *ptr2. Is this the best way to do it? I'm scared its only going to store 1 byte as opposted to the 32-bit address im looking for (since char is only 1 byte)

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    When you declare a pointer of any data type it would normally be 4 bytes or i could say it implementation Dependant you cant just say it going be 2 bytes or bytes. But on my machine which is a 32 bit architecture it allocates 4 bytes. When ever I declare a pointer it allocated 4 bytes to that pointer to hold and address regardless of what Datatype you use. The Data types would normally assigns the scalar value or the offset value. So that when you do pointer + 1. The compiler would know how many bytes it has to go forward.

    >I'm scared its only going to store 1 byte as opposted to the 32-bit address im looking for (since char is only 1 byte)
    I dont think that would be a problem. Reason look above

    ssharish

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    3
    Quote Originally Posted by ssharish2005 View Post
    When you declare a pointer of any data type it would normally be 4 bytes or i could say it implementation Dependant you cant just say it going be 2 bytes or bytes. But on my machine which is a 32 bit architecture it allocates 4 bytes. When ever I declare a pointer it allocated 4 bytes to that pointer to hold and address regardless of what Datatype you use. The Data types would normally assigns the scalar value or the offset value. So that when you do pointer + 1. The compiler would know how many bytes it has to go forward.

    >I'm scared its only going to store 1 byte as opposted to the 32-bit address im looking for (since char is only 1 byte)
    I dont think that would be a problem. Reason look above

    ssharish
    then how does that relate to sizeof(char)? sizeof(char) is 1 byte... shouldnt that be more like 4 bytes?

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    >then how does that relate to sizeof(char)? sizeof(char) is 1 byte... shouldnt that be more like 4 bytes?
    Nope, lets taken an example

    Code:
    char *pte = malloc ( sizeof(char) * 10 );
    What will this do, the compile checks the sizeof char and multiplies with the value 10 and gives your some value. Then malloc will use that value to allocate total bytes. The malloc returns an address to the starting address to that newly allocated memory. So what would be the sizeof address as I said before it is implementation dependent. But on my machine its 4 bytes. Again
    when I do this

    Code:
    ptr++;
    I want pointer to be moves one byte forward but not four bytes, since when you declared a pointer of type char the scaler value was set to 1 bytes. If I do this

    Code:
    int *ptr = malloc(sizeof(int) * 10)
    
    ptr++;
    When I increment the pointer, I want the pointer to be moved 4 bytes forward rather tthan 1 bytes. The value 4 was give by the scaler variable, which was set when you declared the pointer as int.

    malloc expects total bytes which need to allocated. Hope that makes sense.

    ssharish

  5. #5
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Quote Originally Posted by ens_leader View Post
    if I say something like:
    Code:
    ptr2 = malloc(1000) // ignore the 1000, im just allocating excess bytes
    *ptr2 = newImage;
    I'm assuming addresses are 32-bit (meaning they can be expressed in 2-bytes)
    But what im really looking for is, I want to store the memory location (two byte wise) of the current location of newImage in *ptr2. Is this the best way to do it? I'm scared its only going to store 1 byte as opposted to the 32-bit address im looking for (since char is only 1 byte)
    If address are 32 bit then they are 4 byte not 2.I think your basic concern is how will you store these 4 bytes in one byte char ! Well you should have declared a int pointer rather than char.
    If you are forced to use char pointer then you can memcpy the value to char pointer then copy another image in ptr2+4 location and so on.
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    1 byte = 8 bits
    32 bits = 4 bytes

    A pointer address has nothing to do with the data it points to, it's just an address. If someone sends a letter to 123 Main St., unless you've been there, you have no idea if 123 Main St. is a house, a store, an apartment building...

Popular pages Recent additions subscribe to a feed

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