copying pointers

This is a discussion on copying pointers within the C Programming forums, part of the General Programming Boards category; I am practicing with pointers presently, trying to get my knowledge on the matter more robust. I created one program ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    37

    copying pointers

    I am practicing with pointers presently, trying to get my knowledge on the matter more robust.

    I created one program that just uses 2 points and another that uses on string and one array.

    Both compile and work, but I get a warning with the program that uses just pointers. Can someone explain why this is the case. It would be much appreciated.

    program with pointer and array:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    char *pcopy(const char *s, char t[]);
    
    
    int main(void){
    
        char *stringp = "String";
        char stringp2[8];
    
        pcopy(stringp, stringp2);
        printf("\n%s", stringp2);
    
        return 0;
    
    }
    
    char *pcopy(const char *s, char t[] ){
    
        char *p = t;
    
            while(*s  != '\0' )
           {
             *p++ = *s++;
            }
    
        *p = '\0';
    
        return t;
    
    }
    The above works.

    But the following program compiles and runs but there is an error...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    char *pcopy(const char *s, char *t);
    
    
    int main(void){
    
        char *stringp = "String";
        char *stringp2[8];
    
        pcopy(stringp, stringp2);
        printf("\n%s", stringp2);
    
        return 0;
    
    }
    
    char *pcopy(const char *s, char *t ){
    
        char *p = t;
    
            while(*s  != '\0' )
            {
             *p++ = *s++;
            }
    
        *p = '\0';
    
        return t;
    
    }
    The error is :
    ecl.c: In function ‘main’:
    ecl.c:12: warning: passing argument 2 of ‘pcopy’ from incompatible pointer type

    Where exactly am I going wrong here?

    Also my is it that if I change:

    Code:
    char *stringp2[8];
    
    to 
    
    char *stringp2;
    I get a segmentation fault...Is it wrong when using pointer to define a pointer as:

    Code:
    char *stringp2[8];
    Sorry if this post is a little confused I am still hazy in regards to pointers.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The line
    Code:
    char *stringp2[8];
    doesn't give you a pointer. It gives you an array of 8 pointers, none of which point anywhere.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    char *string2[8] is an array of 8 pointers. You should NOT add a star to string2 when you change the copy function.

    --
    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.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    37
    tabstop why can I not define it as:

    Code:
    char *stringp2;
    For an empty string or something...

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Tom Bombadil View Post
    tabstop why can I not define it as:

    Code:
    char *stringp2;
    For an empty string or something...
    What makes you think you can't?

    (Although hopefully by "empty string" you mean "a pointer that points nowhere in particular".)

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    37
    It compiles but gives me a segmentation fault.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Tom Bombadil View Post
    It compiles but gives me a segmentation fault.
    Right. See again, as always, the "doesn't point anywhere" bit. You have to make a pointer point somewhere before you can use it.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Tom Bombadil View Post
    It compiles but gives me a segmentation fault.
    Yes, because the value the pointer has is undefined, and it happens that the value is not somewhere you can copy to.

    Pointers are variables that hold the location of some other memory.

    If we think of memory as the safety-deposit boxes in a bank, then a pointer is a box that contains the number of the box where you ACTUALLY stored your valuables. Of course, if you don't put the number of the valuables in the first box, it's of no use to find the valuables.

    Right now, you have A box to store the number of the other box, but you haven't actually stored anything there.

    If you enable more warnings (and perhaps compile with -O2), the compiler may say "you haven't given string2 a value yet").

    --
    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.

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    37
    So this is the correct way?:

    Code:
        char *stringp = "String";
        char *stringp2;
    
        char ch[7];
    
        stringp2 = &ch;

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Tom Bombadil View Post
    So this is the correct way?:

    Code:
        char *stringp = "String";
        char *stringp2;
    
        char ch[7];
    
        stringp2 = &ch;
    Almost, drop the &, or make it &ch[0]. ch on it's own here will be the address of the first character in the array. As you have written it, you have the address of the address of the first character in the array, which is a pointer to the pointer to a char. [It probably will actually work, because there is no such thing as the address of the address of ch - so the compiler generates the address of ch[0] either way - but it's not right to do it this way].

    --
    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.

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    37
    Understood,

    thanks, much appreciate matsp.

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, 05:23 AM
  2. Copying pointers in structures instead of the structure data?
    By Sparrowhawk in forum C++ Programming
    Replies: 7
    Last Post: 02-23-2009, 06:04 PM
  3. pointers to constants and constant pointers
    By homeyg in forum C++ Programming
    Replies: 1
    Last Post: 06-18-2005, 01:02 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM

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