Thread: char ** problem

  1. #1
    Unregistered
    Guest

    Smile char ** problem

    Hi ,
    I am new to C,
    I create the following function ,to return an array of string
    to main, but there is something wrong in the main part,
    hope somebody can give me a hand.

    char** Array_string(char *,int )
    {

    char *string[256];

    return string;

    }

    void main( )

    {
    char *str[256];

    str=Array_string("testing",4);
    /* at this place is wrong,why */

    }

    Jason

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    few problems if your just passing back a pointer to the string then
    you should probly be either defining the char array in main function otherwise when the function is closed the memory is freed and you could loose your information, the following will work, not 100% what you need:

    Code:
    // What does number do??
    char* Array_string(char *inputstring,char *chararray) 
    { 
      strcpy(chararray,inputstring);
      return chararray
    } 
    
    int main( ) 
    { 
    char str[256]; 
    char *string;
    
    string=Array_string("testing",str); 
    printf("OUT STRING: %s",string);
    return 0;
    }
    hope this helps you understand, basically before when you do:
    char *str[256];
    it actually creates 256 pointers in an array no character memory is defined.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > char** Array_string(char *,int )
    > {
    >
    > char *string[256];
    >
    > return string;

    The problem is you are returning a local variable, which is destroyed when you leave the scope of the function.
    Code:
    char** Array_string( char *someChar, int someInt )
    {
         char **c;
    
         /* create 'someInt' number of character pointers */
         c = malloc( sizeof(char*) * someInt );
    
        /* return them */
        return c;
    }
    That will work much better.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Unregistered
    Guest

    char ** problem

    Oh, this is what i want.

    struct member{
    char lastName[60];
    char firstName[60];
    char id[8];
    int year;
    struct memberRecord* next;

    typedef struct member memberRecord;
    memberRecord *records
    /**
    This function is to return the member id that belongs to a particular year


    */
    char** idYear(memberRecord* records, int year) {


    return

    }


    void main()
    {

    str =idYear(records,year);
    }

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    char* idYear(memberRecord* records, int year) { 
       int i;
       for (i=0; i<NUM_RECORDS; i++)
       {
          if (records[i].year == year)
             return records[i].id;
       }
       return NULL;
    } 
    
    
    void main() 
    { 
    char *str;
    
    str =idYear(records,year); 
    if (str == NULL)
       printf("Record not found.\n");
    }

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by swoopy
    Code:
    void main() 
    { 
    char *str;
    
    str =idYear(records,year); 
    if (str == NULL)
       printf("Record not found.\n");
    }
    That first line of code is the real reason that Sayeh is leaving. That and "How do I clear the screen?"

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Unregistered
    Guest
    This only return one record of year, for about an array of year, that is more than one. How to do this char** ?
    May be need to use
    malloc( sizeof(char*) * someInt ?

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Originally posted by Unregistered
    This only return one record of year, for about an array of year, that is more than one. How to do this char** ?
    May be need to use
    malloc( sizeof(char*) * someInt ?
    Yes, to return an array of year, you need to use malloc(). Look at Quzah's code above.

  9. #9
    Unregistered
    Guest
    This only return one record of year, for about an array of year, that is more than one. How to do this char** ?
    First figure out what you want. Your code tells me that you don't know, also the structure is not correct.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  2. char problem
    By ForlornOdium in forum C++ Programming
    Replies: 10
    Last Post: 10-29-2003, 02:39 PM
  3. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  4. String Processing Problem
    By muffin in forum C Programming
    Replies: 0
    Last Post: 08-24-2001, 10:13 AM