Thread: Beginner Help with returning a string of arrays

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    7

    Beginner Help with returning a string of arrays

    Hi al, Ive played with C like 5 or 6 years ago but i completely forgot how it works untill recently i'm doing it for a fun project. Anyway heres my code. randstr() will return a 6 char random string. I can printf the results on the randstr() functoin,but whenever i pass the value back to main() to be prinft. i get a lot of weird values. Englighten me!! thanks in advance

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    
    
    char *randstr()
    {
    
    	char tempstr[6];
        char rnd;
    
    
        /* Set evil seed (initial seed) */
        srand( (unsigned)time( NULL ) );
    
        for (int i = 0; i < 6; i++) {
            rnd = ((float) rand()/RAND_MAX) * 25 + 97;
    		tempstr[i] = rnd;
        }
        tempstr[6] = '\0';
    //	printf("%s\n",tempstr);
        return tempstr;
    }
    
    
    
    
    int main()
    {
    
    
    printf("%s",randstr());
    return 0;
    }

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    When you do char tempstr[6]; inside a function, you are declaring an automatic variable... Once the function exits, the contents of the memory at that address are no longer defined, it's out of scope.

    Your options are:

    1) use malloc to declare a chunk of memory, then return the pointer to that data to the calling code. The calling code will then have to call free on it when it's done. (This is a little silly)

    2) pass the array (you are passing the address of the first element) to the function from your calling code, and have the function fill the array. (This is more sensible)

    3) declare the array inside your function static, so that it persists even when the function exits. The problem with this method is that if you call it twice, the second call overwrites the result of the first, because you just have a single static array.
    Last edited by cwr; 12-08-2005 at 07:22 PM.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    7
    thanks man! got it. you are from my to! u must be a C sifu

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    7
    man .C is tough .. anyway why is this not working ?? why do i have to declare char login[50]; instead of char *login; ? what is the difference between both of them? i thought it's the same




    Code:
    char *randstr()
    {
    
    	static char tempstr[6];
        char rnd;
    
    
        /* Set evil seed (initial seed) */
        srand( (unsigned)time( NULL ) );
    
        for (int i = 0; i < 6; i++) {
            rnd = ((float) rand()/RAND_MAX) * 25 + 97;
    		tempstr[i] = rnd;
        }
        tempstr[6] = '\0';
    //	printf("%s\n",tempstr);
        return tempstr;
    }
    
    
    
    
    int main()
    {
    char *login;   <--- here 
    
    	      strcpy(login,"NICK ");
    		  strcat(login,randstr());
    		  strcat(login," 8 * :abe abe\n");
    		  printf("login var = %s",login);
    
    
    return 0;
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > for (int i = 0; i < 6; i++)
    Standard C doesn't support declarations in for loops (C99, C++ and compiler specific extensions needed for this).

    > tempstr[6] = '\0';
    C array subscripts are from 0 to n-1, this is one off the end of the array.

    > strcpy(login,"NICK ");
    login isn't pointing anywhere - use an array, not a pointer
    eg char login[100];

  6. #6
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by yuliang11
    thanks man! got it. you are from my to! u must be a C sifu
    Sorry, I'm completely unable to decipher the last two sentences. What is your to? And what is a sifu?

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    7
    1) "you are from my to! " = my ?

    Yer location, Location: Kuala Lumpur, Malaysia ?


    2 ) "sifu" = master (cantonese )



  8. #8
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Sorry, yes, I asked a local and they told me what sifu meant. I'm not from KL, obviously, I just live here at the moment. :-)

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You'll also need <stdio.h> and <time.h>.

    See Salem's post.

    [edit]
    And <stdlib.h>. Didn't see rand().
    [/edit]
    Last edited by dwks; 12-10-2005 at 03:08 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. Separate long string into multiple arrays
    By cashmerelc in forum C Programming
    Replies: 6
    Last Post: 11-27-2007, 02:57 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. Replies: 3
    Last Post: 12-03-2001, 01:45 PM