Thread: reading a return string value?

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    53

    reading a return string value?

    I have written a function to return a unique id for my linked list. I have run it through the debugger and checked the watcher that the values that are returned are correct.

    Code:
    char* generatedID(GJCType* menu) {
    
        CategoryType* current;
        char newCatID[ID_LEN + 1] = {0};
        char catID[ID_LEN + 1] = {"C\0\0\0\0\0"};
        char* catIDPtr = catID;
        int index, idDigit;
        current = menu->headCategory;
    
        for (index = 1; index <= menu->numCategories; index++) {
            sprintf(newCatID, "%04i", index);  /* newCatID = 000# */
            strncat(catIDPtr, newCatID, ID_LEN + 1); /* catID = char */
            if (strcmp(catID, current->categoryID) == 0) {
                current = current->nextCategory;
                strncpy(catID, "C\0\0\0\0\0", ID_LEN + 1);
            } else {
                idDigit = index;
                break;
            }
        }
        if (index > menu->numCategories) {
           sprintf(newCatID, "%04i", index);  /* newCatID = 000# */
          strncat(catIDPtr, newCatID, ID_LEN + 1); /* catID = char */
          catID[ID_LEN + 1] = '\0';
        }
        return *catID;
    }
    Code:
    char* catIDPtr;
    catIDPtr = generatedID(menu);
    
        printf("New category ID is %s.\n", catIDPtr); /* causes an error */

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should actually ask a question in your post next time. Oh, and start compiling with warnings on:
    Code:
        char catID[ID_LEN + 1] = {"C\0\0\0\0\0"};
    That is local to this function. It is destroyed when this function ends.
    Code:
    return *catID;
    That, if we pretend it is valid, would return a single character, not a string.

    You probably mean to be allocating something with say... malloc, and returning that, and of course free-ing it when you are done.


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

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    53
    Just so you know. I HATE C. Can't wait till i finish this semester.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by TonyG View Post
    Just so you know. I HATE C. Can't wait till i finish this semester.
    Boy if ever there was a wrong place to say that!

    One of the toughest lessons to learn here is that variables inside a function are destroyed when the function exits. Returning pointers to variables inside a function results in a ticking bomb just waiting to go off... It may work the first few times, but then it'll misfire as the stack space is reused and changed for other variables... and of course these things always happen when teach is grading assignments...

    Why you should never return pointers to arrays ....
    Code:
    #include <stdio.h>
    
    int* MyFunction(int a, int b, int c)
      {  static int array[3];
         array[0] = a;
         array[1] = b;
         array[2] = c;
         return array;  } // return a pointer.
    
    
    int main (void)
      { int *a1, *a2;  // int pointers
    
        printf("calling a1 = MyFunction(10,20,30);\t");
        a1 = MyFunction(10,20,30);
        printf("a1 has %d %d %d\n",a1[0],a1[1],a1[2]);
    
        printf("calling a2 = MyFunction(100,200,300);\t");
        a2 = MyFunction(100,200,300);
        printf("a2 has %d %d %d\n",a2[0],a2[1],a2[2]);
    
        printf("\nLooks good, except...\t"); 
        printf("a1 now has %d %d %d\n",a1[0],a1[1],a1[2]);
    
        getchar();
        return 0; }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I return a string from a C dll to C#?
    By MAtkins in forum C Programming
    Replies: 5
    Last Post: 05-07-2011, 04:59 PM
  2. Replies: 12
    Last Post: 04-07-2007, 11:11 AM
  3. Replies: 6
    Last Post: 04-09-2006, 04:32 PM
  4. Replies: 4
    Last Post: 01-30-2006, 05:04 AM