Thread: pointers of chars and arrays of chars Question

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    120

    pointers of chars and arrays of chars Question

    hi

    i noticed something while working with arrays, if i do this:

    Code:
    int main()
    {
        char* a="abc";
    
        printf("%p %p %p %p", a, &a[0], &a[1], &a[2]);
    
        a="cba";
    
        printf("\nsecond values: %p %p %p %p", a, &a[0], &a[1], &a[2]);
    }
    the values in the "second values" are diferent, meaning that a[0] changed its position.

    so this means that everytime i change an array like this: a="HELLO WORLD" the a[0] will change its position??
    Last edited by shiroaisu; 08-08-2010 at 09:22 PM.

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    a isn't an array. It's a pointer. You are indexing into whatever memory location it points to. So when you change where it points to, then the offsets from that base will also likely be different (IE: &a[0], &[a1], etc..).

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    120
    So that means that if i have a="b" that would be like making 'a' point to the address number "b" (witch i think it gives a=4206666 because i casted it to an int)??
    Last edited by shiroaisu; 08-08-2010 at 09:46 PM.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Quote Originally Posted by shiroaisu View Post
    So that means that if i have a="b" that would be like making 'a' point to the address number "b" (witch i think it gives a=4206666 because i casted it to an int)??
    You aren't casting a char pointer to an int. A pointer is a 32 bit (can be 64 on x64 systems??? donno for sure) integer to begin with. Just because it is a CHAR pointer, doesn't mean the variable/object is still a CHAR. It is still a pointer. It just gets interpreted as that object type if you were to do something like cout<< (*a) <<endl; and it also tells how many bytes the memory location is.

    Try this once...

    Code:
    cout<< sizeof(char) <<endl;
    cout<< sizeof(char*) <<endl;
    cout<< sizeof(int) <<endl;
    cout<< sizeof(int*) <<endl;
    cout<< sizeof(double) <<endl;
    cout<< sizeof(double*) <<endl;
    Pointers are memory locations. That 0x04206666 value you saw is a location in memory.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    120
    Quote Originally Posted by scwizzo View Post
    You aren't casting a char pointer to an int. A pointer is a 32 bit (can be 64 on x64 systems??? donno for sure) integer to begin with. Just because it is a CHAR pointer, doesn't mean the variable/object is still a CHAR. It is still a pointer. It just gets interpreted as that object type if you were to do something like cout<< (*a) <<endl; and it also tells how many bytes the memory location is.

    Try this once...

    Code:
    cout<< sizeof(char) <<endl;
    cout<< sizeof(char*) <<endl;
    cout<< sizeof(int) <<endl;
    cout<< sizeof(int*) <<endl;
    cout<< sizeof(double) <<endl;
    cout<< sizeof(double*) <<endl;
    Pointers are memory locations. That 0x04206666 value you saw is a location in memory.
    yes, i know all of that, what i meant to say is that i did like this: int k="b" and it gave me that number 4206666
    so doing char* a="b" would be the same that char* a=4206666 right?

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    Argh, I'm wrong. Your pointer should be fine... Forget my nonsense.

    The reason it's different is because you are setting the pointer to point to "abc" and then to "cba" which are different string literals at different locations in the program's memory. So, naturally, they are different memory addresses. In fact, all of the positions should be different in the second case.
    Last edited by syzygy; 08-09-2010 at 09:41 AM.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by scwizzo View Post
    You aren't casting a char pointer to an int. A pointer is a 32 bit (can be 64 on x64 systems??? donno for sure) integer to begin with. Just because it is a CHAR pointer, doesn't mean the variable/object is still a CHAR. It is still a pointer. It just gets interpreted as that object type if you were to do something like cout<< (*a) <<endl; and it also tells how many bytes the memory location is.
    Naturally. Otherwise it can't address >4 GB of memory.
    Also, are you doing C or C++?
    int k="b" will not compile with C++. Nor should you be using printf for that matter.
    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.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    120
    Quote Originally Posted by syzygy View Post
    Argh, I'm wrong. Your pointer should be fine... Forget my nonsense.

    The reason it's different is because you are setting the pointer to point to "abc" and then to "cba" which are different string literals at different locations in the program's memory. So, naturally, they are different memory addresses. In fact, all of the positions should be different in the second case.
    but w8, that means that the pointers location, the location of a, will change acording to the inputed string??

    and btw im doing c now.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, a string literal is stored in a far-away place and when you try to reference it, it decays to a pointer.
    When you input data, you are writing data to a memory location.
    String literals are already written somewhere. You just take the address where they are stored.

    And if you are doing C, perhaps you shouldn't post in the C++ forum?
    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.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    120
    Quote Originally Posted by Elysia View Post
    No, a string literal is stored in a far-away place and when you try to reference it, it decays to a pointer.
    When you input data, you are writing data to a memory location.
    String literals are already written somewhere. You just take the address where they are stored.

    And if you are doing C, perhaps you shouldn't post in the C++ forum?
    ok then, i think i understand what happens now.

    as for the forum, well, at first i didnt realized that i was posting in this forum so it was a mistake, but then i thought that maybe it isnt so bad because the subject is pointers and ill move on to c++ soon so ppl would also give me warnings like the ones that u gave me about int b="o" not working for example

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A lottery program - arrays, ints, chars etc
    By Glauber in forum C++ Programming
    Replies: 13
    Last Post: 05-25-2008, 10:48 AM
  2. Arrays question
    By tlovaj in forum C Programming
    Replies: 19
    Last Post: 02-12-2008, 09:08 PM
  3. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  4. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  5. Array pointers to Multi-Dimensional Arrays
    By MethodMan in forum C Programming
    Replies: 3
    Last Post: 03-18-2003, 09:53 PM