can c let me pass string references??

This is a discussion on can c let me pass string references?? within the C Programming forums, part of the General Programming Boards category; I want to pass a reference to a string variable (char[]) to a funtion and have the function change the ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    1

    Question can c let me pass string references??

    I want to pass a reference to a string variable (char[]) to a funtion and have the function change the original value of that string - but I CANNOT get this to work. I am going mad - any help?

    See following little prog which I think SHOULD work but doesn't!! It changes the string variable to some random char??

    #include <stdio.h>

    void changeString(char *str[]);

    void main()
    {
    char string[]="NO";

    printf("\nString: %s\n\n", string);
    changeString(&string);
    printf("Changed String: %s\n\n", string);

    }

    void changeString(char *str[])
    {
    *str="YES";
    printf("Change to: %s\n\n", *str);
    }

  2. #2
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Take out the star *, a string is passed as a pointer. Either use char *str or else char str[]. I would choose the later. This is wrong char *str[]

  3. #3
    Unregistered
    Guest

    Thumbs down

    Thanks for reply - as this is driving me nuts!

    But both your suggestions cause my exe to crash when I run it - giving an application error saying something "the memory cant be read".

    Any more suggestions? Would really appreciate your help asap?

  4. #4
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    That's because you probably have to change your code! You want me to write it for you!! I am not wrong.

  5. #5
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Code:
    #include <stdio.h> 
    #include <string.h>
    
    void changeString(char str[]); 
    
    void main() 
    { 
    char string[]="NO"; 
    
    printf("\nString: %s\n\n", string); 
    changeString(string); 
    printf("Changed String: %s\n\n", string); 
    
    } 
    
    void changeString(char str[]) 
    { 
    	strcpy(str,"YES"); 
    	printf("Change to: %s\n\n", str); 
    }

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Congratulations, you just found the pointer equivalent to a boogeyman.

    This involves talking about the difference between a pointer ad an array... if you were to change
    char string[]="NO"
    to
    char * string = "NO"
    Then the program would work... (well, kinda).

    This is because the first declares string as an array, and the second declares string as a pointer. The problem happens when you get to this line...

    changeString(&string);

    Clearly, you're going to change the value of string with this command. Now, when you & a pointer variable, you get the memory address of that pointer, which is how & is supposed to work.
    When you & an array (a pointer constant), since it's a constant, it doesn't have any memory address; it's hardcoded into your program. Typically, trying to do this is meaningless. I mean, it's like saying pop (&1); you just can't use & on a constant. With arrays however, C allows it (it shouldn't). When you use & on an array, it just acts as if you had passed the array. Now let's look into your changeString function...
    Code:
    void changeString(char *str[]) 
    { 
    *str="YES"; 
    printf("Change to: %s\n\n", *str); 
    }
    Let's run through this code, with letting str have the same value as string in main. "YES" returns a pointer to a string that has been temporarily placed in memory. We store this pointer in *str. Since str is the same thing as string (if we used string as an array), the memory that's being pointed to is our string itself. So, whatever was in our string ("NO") gets overwritten by a pointer representing wherever the string "YES" is located in memory.

    printf("Change to: %s\n\n", *str);
    Now, *str contains a pointer, so this line has no problem following that pointer to print the string "YES"

    But then we leave the function, and try this...
    printf("Changed String: %s\n\n", string);
    Again, *string contains a pointer, so when we try to point the chars in *string, we end up printing arbitrary characters.

    Something to point out however, is that if you change string to a char *, then although your program will work, it's not gauranteed to work. When it hits the "YES" your program goes to a static buffer, puts "YES" in it, and returns a pointer to that buffer. The program may, however, change the contents of that buffer as it sees fit. The technically correct way to go about this would be to use the strcpy function.

    Also, main is supposed to return an int, not void
    Callou collei we'll code the way
    Of prime numbers and pings!

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,825
    > strcpy(str,"YES");
    Oops - but str has only been allocated 3 bytes, and this copies 4 bytes

    In main, you should have at least
    char string[4]="NO";

  8. #8
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Okay, that is true, I wasn't counting!

  9. #9
    oeleboele
    Guest

    Lightbulb

    How about this then ?

    #include <stdio.h>

    void changeString(char *str);

    int main()
    {
    char string[4]="NO";

    printf("\nString: %s\n\n", string);
    changeString(string);
    printf("Changed String: %s\n\n", string);
    return 0;
    }

    void changeString(char *str)
    {
    *str='Y';
    str++;
    *str='E';
    str++;
    *str='S';
    str++;
    *str='\0';
    }


    You know, Im just started learning C and had the same question about passing arrays and was about to post when I read this. Started playing and now I (hopefully) understand (althoug i would use the cpystr too... Honest!! )

  10. #10
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Sure you can use pointer arithmetic if you want since the pointer is pointing to a sizeof(char) address space. Should not be a problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 02:02 AM
  3. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 06:13 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM

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