Thread: Sending String to Function not working

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    20

    Sending String to Function not working

    I am trying to pass a string to this function:

    Code:
    void insert(ListNodePtr *sPtr, char *value);
    I am gathering the string from scanf, printing it, and then passing it to the function like so (I am building a linked list):

    Code:
    struct listNode
    {
       char *data;
       struct listNode *nextPtr;
    };
    
    typedef struct listNode ListNode;
    typedef ListNode *ListNodePtr;
    
    int main(void)
    {
       
       ListNodePtr startPtr = NULL;
       char *zztop;
    
       scanf("%s", &zztop);
    
       printf("\n%s\n", &zztop);
    
       insert(&startPtr, zztop);
    }
    
    void insert(ListNodePtr *sPtr, char *value)
    {
       ListNodePtr newPtr;
       ListNodePtr previousPtr;
       ListNodePtr currentPtr;
    
       newPtr = malloc(sizeof(ListNode));
    
       printf("\n %s \n", &value);
    }
    The printf value at the end of the insert function is merely a checkpoint to see if the data that was given to function insert is correct. However, random garbage is printed. Why?

    My apologies if the question is convoluted, hope this makes some sense! Thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    There's nothing meaningful that can be printed from &value, much as there's nothing meaningful that can be printed from &zztop. In fact if you lose all those & you'll be a lot better off.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    20
    The first printf statement inside main seems to work fine. How should I be printing zztop there otherwise?

    I removed the ampersand before value in the second printf statement located in the insert function. It still seems to print garbage.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
       char *zztop;
    
       scanf("%s", &zztop);
    You didn't allocate memory to hold your string.

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    You have the same problem inside your node as well. Additionally, do not typedef pointers. It is a bad practice for almost all cases (exceptions being opaque pointers and only few other specific cases)
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    20
    Oh, so does that mean that the string entered will be lost and unreachable outside of the main function? How do I avoid this, or how do I allocate memory for this? I can't imagine I'd be using a malloc......or would I?

  7. #7
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Quote Originally Posted by voidpain() View Post
    Oh, so does that mean that the string entered will be lost and unreachable outside of the main function? How do I avoid this, or how do I allocate memory for this? I can't imagine I'd be using a malloc......or would I?
    Try
    Code:
    char zztop[BEARD];
    where BEARD is one more than the length of the longest string you will ever want to enter (leaves room for the '\0').
    Code:
    while(!asleep) {
       sheep++;
    }

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    You could either use malloc or make a char array. As I said before, same thing applies for your node definition. A simple way:
    Code:
    struct NODE{
         char myData[20]; //allocate enough room for 19 characters plus 1 '/0' character
         struct NODE* next;
    };
    int main(void){
    
         char input[20]; //allocate enough room for 19 characters plus 1 '/0' character
    
         printf("Enter input: ");
         .....
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by voidpain() View Post
    I am trying to pass a string to this function:
    I am gathering the string from scanf, printing it, and then passing it to the function like so (I am building a linked list):
    Code:
    struct listNode
    {
       char *data;                     <---- no memory allocated
       struct listNode *nextPtr;
    };
    
    typedef struct listNode ListNode;
    typedef ListNode *ListNodePtr;
    
    int main(void)
    {
       
       ListNodePtr startPtr = NULL;
       char *zztop;                    <--- no memory allocated
    
       scanf("%s", &zztop);
    
       printf("\n%s\n", &zztop);
    
       insert(&startPtr, zztop);
    }
    
    void insert(ListNodePtr *sPtr, char *value)
    {
       ListNodePtr newPtr;
       ListNodePtr previousPtr;
       ListNodePtr currentPtr;
    
       newPtr = malloc(sizeof(ListNode));
    
       printf("\n %s \n", &value);
    }
    What you need to do is use char arrays not pointers... as in char zztop[100]... and in your struct... char data[100] ... (or whatever the correct size is)

    What is happening is that your scanf function is writing to memory you don't own and you are storing your string in memory you don't own.

    Step 1 ... forget the list for the moment... get your software to read and display the user input. When you have that working you can move on to creating your first node in the list...

  10. #10
    Registered User
    Join Date
    Aug 2011
    Posts
    20
    I actually previously implemented this as char zztop[30], and I got it to print just fine within main. But how exactly am I going to pass zztop[30] into the insert function? I can't pass an array like that into a function....or can I? ....or do I use a pointer to the array, or something?

  11. #11
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    You can pass the address of the first element of the array to a function; "&zztop[0]" and "zztop" are the same in this case. The function will not know how many elements are in it, but that's OK. That's what the '\0' character is for.
    Code:
    while(!asleep) {
       sheep++;
    }

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by voidpain() View Post
    I actually previously implemented this as char zztop[30], and I got it to print just fine within main. But how exactly am I going to pass zztop[30] into the insert function? I can't pass an array like that into a function....or can I? ....or do I use a pointer to the array, or something?
    The array's name decays to a pointer when passed to a function... So test the proposition...
    Code:
    void PrintStr(char *str)
      { printf("%s", str); }
    
    // call as
    PrintStr(zztop);
    ... and see what happens.

    When in doubt... Experiment!

  13. #13
    Registered User
    Join Date
    Aug 2011
    Posts
    20
    Also, just wanted to mention how bad ass this forum is. I'm in a summer school class right now, and the school's Help Center has been closed all summer. So far, this forum has been extremely helpful...I apreesh, thank you

  14. #14
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Yes you can, just pass the array as an argument to your function. There is nothing preventing that.

    Code:
    void foo(char*);
    
    int main(void){
    
         char myString[20];
    
         .......
         foo(myString);
    .....
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  15. #15
    Registered User
    Join Date
    Aug 2011
    Posts
    20
    Works perfectly. Exactly what I was looking for. Thanks to all!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 05-07-2010, 03:53 AM
  2. Sending a string to another computer
    By guitarist809 in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-22-2007, 11:18 PM
  3. function that copies token from a string not working
    By Kinasz in forum C Programming
    Replies: 6
    Last Post: 08-07-2004, 12:34 AM
  4. Sending a String to a Window
    By X PaYnE X in forum Windows Programming
    Replies: 5
    Last Post: 03-31-2004, 12:32 PM
  5. Sending a string to C++ from VB 6.
    By VirtualAce in forum C++ Programming
    Replies: 4
    Last Post: 08-21-2001, 02:28 AM

Tags for this Thread