Thread: changing the address of a pointer question..

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    changing the address of a pointer question..

    Code:
    string t="rrrrr";
    string *p=&t;
    strcpy(p, "abc");
    i was told that it tells P to point on the data which on the address "abc"

    i thought that in order to change the address of a pointer we need to add &

    strcpy(&p, "abc");

    ???

    (i know that changing the address into "abc" is not recommended)
    Last edited by transgalactic2; 10-14-2008 at 03:24 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't change the address of a pointer (or of any variable). Once you declare the variable, it's address is firmly set, once and forever.

    You can, however, change the value of a pointer, just as easily as you can change the value of an int: x = 10 or p = &t.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Your example has nothing to do with changing the address. Plus string is not defined in C (just in C++)

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by transgalactic2 View Post
    i thought that in order to change the address of a pointer we need to add &

    strcpy(&p, "abc");

    ???
    All that's doing is placing the character string "abc" in an array of byte cells whose starting location is contained in the variable &p.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    A pointer has an address like other variables (which can't and needn't be changed) but what makes a pointer a pointer is that it's value is an address -- the address of the variable it points to (which means this address has to be a different one; *ptr=&ptr won't work).

    A confusing thing about c is the difference between the syntax referencing int's and char's. Probably this is because char declarations (eg, char word[]="this") are usually arrays for strings, so you are allowed to initialize them using a "string constant" ("this") AND refer to that constant using the name of the array. With an int, the name of an array defaults to pointing to it's first element (I'll explain why at the end). Look at this:

    Code:
    #include <stdio.h>
    
    int main () {
    	int intray[5]={'t','h','i','s','\0'};
    	char charray[5]={'t','h','i','s','\0'};
    	printf("using intray: &#37;s\n", intray);
    	printf("using charray: %s\n", charray);
    }
    the output
    using intray: t
    using charray: this


    So strcpy(&p, "abc"); is something you can't (and would never need to) do.

    The "&" gives you the address of a variable, the same address a pointer to that variable would hold. It's usually used with single integer variables and char array names so that those variables can be passed to a function and modified without having to be returned or globalized.

    Code:
    #include <stdio.h>
    
    void exampfunc (int *n) {
    	*n=5;
    }
    
    int main () {
    	int x=0;
    	exampfunc(&x);
    	printf("%d\n",x);
    }
    Of course the output is 5. That's because we passed the function a "pointer" (&x, the address of x). So in examplfunc, &x is *n, a pointer to some number. That means we can change the value at the address directly, rather that having to return a value for assignment. With a char array (which is more common) it looks different:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void exampfunc (char *n) {
    	strcpy(n,"that");
    }
    
    int main () {
    	char word[]="this";
    	exampfunc(word);
    	printf("%s\n",word);
    }
    No & and only one *, but the output is still that.

    string t="rrrrr";

    You can't work strings this way in C. You have to think of them as char arrays. When you declare a char pointer (char *word) you can use strcpy on it after you assign it some memory, since you actually need multiple memory addresses to store multiple letters -- but the pointer *word will include all the spaces assigned to it. Since you don't assign int's memory, their pointer's only cover the first value of an array.

    So to conclude: strcpy(&p, "abc"); is impossible because "abc" requires an array. You can't reference anything but a character array, and for that you can't use "&".
    Last edited by MK27; 10-14-2008 at 05:17 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by MK27 View Post
    A pointer has an address like other variables (which can't and needn't be changed) but what makes a pointer a pointer is that it's value is an address -- the address of the variable it points to (which means this address has to be a different one; *ptr=&ptr won't work).
    That might work, depending on what ptr points to (if it points to an int, then hurray! if it points to a char, then boo!). ptr=&ptr will always work (leaving out architecture issues, should anyone be using a computer where different pointers have different sizes).

    Quote Originally Posted by mk27 View Post
    A confusing thing about c is the difference between the syntax referencing int's and char's. Probably this is because char declarations (eg, char word[]="this") are usually arrays for strings, so you are allowed to initialize them using a "string constant" ("this") AND refer to that constant using the name of the array.
    There are no syntax difference between int's and char's. Yes, people often throw around char * and char[] as strings, but there's no syntax here.

    Quote Originally Posted by mk27 View Post
    With an int, the name of an array defaults to it's first element (I'll explain why at the end).
    This is simply untrue. The name of an array (no matter what base type) always and forever defaults to a pointer to its first element (NOT the value of its first element). (Except in sizeof, but that's a keyword and not an evaluation context.)
    Quote Originally Posted by mk27 View Post
    Look at this:

    Code:
    #include <stdio.h>
    
    int main () {
    	int intray[5]={'t','h','i','s'};
    	char charray[5]={'t','h','i','s'};
    	printf("using intray: &#37;s\n", intray);
    	printf("using charray: %s\n", charray);
    }
    the output
    using intray: t
    using charray: this
    All this example shows is that you have a little-endian system. If you had a big-endian system, intray would have printed nothing at all! The "reason" you can't print an integer array as though it were a string like this is that sizeof(int) != 1, so 't' in an int array takes up two/four/eight bytes (one/three/seven of which are zero, which act in a string context as null terminators). And on a little-endian system, you'll find the significant bits (where the zeroes are) last.

    Quote Originally Posted by mk27 View Post
    So strcpy(&p, "abc"); is something you can't (and would never need to) do.

    The "&" gives you the address of a variable, the same address a pointer to that variable would hold. It's usually used with single integer variables and char array names so that those variables can be passed to a function and modified without having to be returned or globalized.

    Code:
    #include <stdio.h>
    
    void exampfunc (int *n) {
    	*n=5;
    }
    
    int main () {
    	int x=0;
    	exampfunc(&x);
    	printf("%d\n",x);
    }
    Of course the output is 5. That's because we passed the function a "pointer" (&x, the address of x). So in examplfunc, &x is *n, a pointer to some number. That means we can change the value at the address directly, rather that having to return a value for assignment. With a char array (which is more common) it looks different:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void exampfunc (char *n) {
    	strcpy(n,"that");
    }
    
    int main () {
    	char word[]="this";
    	exampfunc(word);
    	printf("%s\n",word);
    }
    No & and only one *, but the output is still that.
    Note that the proper integer code that pairs with your character code is
    Code:
    void exampfunc(int *n) {
        n[2] = 7;
    }
    
    int main() {
        int numbers[] = {1, 2, 3, 4, 5};
        exampfunc(numbers);
        printf("%d\n", numbers[2]);
        return 0;
    }
    Note again no & and one *.

    Quote Originally Posted by mk27 View Post
    string t="rrrrr";

    You can't work strings this way in C. You have to think of them as char arrays. When you declare a char pointer (char *word) you can use strcpy on it after you assign it some memory, since you actually need multiple memory addresses to store multiple letters -- but the pointer *word will include all the spaces assigned to it.
    I like nearly all of this. (The end worries me a tad; a pointer's just a pointer, but we know we own the memory behind it, so I guess it's ok.)

    Quote Originally Posted by mk27 View Post
    Since you don't assign int's memory, their pointer's only cover the first value of an array.
    Eh. See above.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    There are no syntax difference between int's and char's. Yes, people often throw around char * and char[] as strings, but there's no syntax here.
    Oh Yes There Is!

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main () {
    	int y=7, *iptr=&y;
    	char x='X', *cptr=&x;
    	
    	printf("%d %c\n",*iptr,&cptr);
    }
    Now, if you change 7 to 'X' you can change &cptr to *cptr (and %c to %d), and then it looks the same because both are 88. But after you do that, try changing the first %d to %c and *iptr to &iptr. If that worked, you'd have a transparently symmetrical relationship in the syntax.

    Quote Originally Posted by tabstop View Post
    This is simply untrue. The name of an array (no matter what base type) always and forever defaults to a pointer to its first element (NOT the value of its first element). (Except in sizeof, but that's a keyword and not an evaluation context.)
    You're right, and I changed the text of the orginal post to read "defaults to pointing to".


    Quote Originally Posted by tabstop View Post
    Eh. See above.
    I don't think you're actually disagreeing with me -- could be a matter of syntax.
    Last edited by MK27; 10-14-2008 at 05:43 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by MK27 View Post
    Oh Yes There Is!

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main () {
    	int y=7, *iptr=&y;
    	char x='X', *cptr=&x;
    	
    	printf("%d %c\n",*iptr,&cptr);
    }
    Now, if you change 7 to 'X' you can change &cptr to *cptr (and %c to %d), and then it looks the same because both are 88 now -- which means it's actually different :0, because that's not what was going on before. After you do that, try changing the first %d to %c and *iptr to &iptr. If that worked, you'd have a transparently symmetrical relationship in the syntax.
    What?
    Running the original gives me
    7 p
    Changing 7 to 'X' and &cptr to *cptr gives me
    88 88
    Making the second set of changes (both to &) gives me
    x p

    I fail to see how "printing a 32-bit pointer as though it was an 8-bit character screws things up" translates into "ints and chars have different array syntax".

    I did this:
    Code:
    int main () {
            int y='X', *iptr=&y;
            char x='X', *cptr=&x;
    
            printf("%c %c\n",*iptr,*cptr);
    }
    and got X X like I would expect.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    What?
    I fail to see how "printing a 32-bit pointer as though it was an 8-bit character screws things up" translates into "ints and chars have different array syntax".
    I fail to see how it doesn't, which is what I demonstrated. If you run the example with &iptr over again, you'll notice the value changes because &iptr is still just an address. You're final example is exaclty the same as the first change -- the one I said "makes it look the same". You could also consider their syntax the same because they both use a single syllable abbreviation, which is true. You can't prove your point with positive examples -- you have to demonstrate there are any contrary ones, so please ignore

    printf("%c %c", &iptr, &cptr);
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your point was, as I recall, that you can "refer [to the value of the string] using the name of the array". I say (paraphrasing) that's baloney, because the name of the array always is a pointer to the first element of an array (regardless of whether it is an array of char or of int). So in this code
    Code:
    char name[10] = "Babalu";
    name, by itself, does not refer to "Babalu". name, by itself, does not refer to 'B'. It refers to 0xwhatever, the address in memory where the first element of the name array is stored. If you print it with %c, as in
    Code:
    printf("%c", name);
    you won't get 'B' (unless you are very lucky).

    And (which was my point) everything I just said would be equally true if it was int name[10].

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You can refer the value of a string using the name of an array, because the name is a pointer to a chunk of memory of an allocated size containing the string!

    And my point was there's a syntax difference, which is why my first example for transgalactic didn't read:

    Code:
    int intray[5]="this";
    char charray[5]="this";
    Maybe this is symptomatic of how "printing a 32-bit pointer as though it was an 8-bit character screws things up".
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Ah, I see. There is a difference when you initialize them, yes. I foolishly took your statement about referencing arrays to mean referencing arrays.

  13. #13
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    this is a quote from an article:
    "
    int foo;
    int *foo_ptr = &foo;

    Now, how do you assign an int to this pointer? This solution might be obvious:

    foo_ptr = 42;

    It is also wrong.

    Any direct assignment to a pointer variable will change the address in the variable, not the value at that address. "

    here its been said that i can change the address of a pointer



    ???

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, you can't change the address of a pointer (look again!).
    It says you can change the address in the variable.
    A pointer simply holds a memory address, so of course you can change that.
    But you cannot change the address of a variable (the address where the variable is located).
    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.

  15. #15
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Unless you specifically know how or why to do otherwise, you shouldn't even ever directly set a pointer to a constant value other than zero.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Changing pointer inside function?
    By DrSnuggles in forum C++ Programming
    Replies: 2
    Last Post: 07-20-2008, 05:10 AM
  3. Pointer to specific memory address
    By elnerdo in forum C++ Programming
    Replies: 10
    Last Post: 05-19-2006, 07:35 AM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. Im so lost at . .
    By hermit in forum C Programming
    Replies: 18
    Last Post: 05-15-2002, 01:26 AM