Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2003
    Posts
    3

    Unanswered: C Program: FUNCTION failed to return ARRAY values

    Hi...

    I have some problem regards to my C programming ...(refer as below )

    As your info, my program did not return any numbers of the ARRAY VALUE's...it's just return NULL, as example :
    ==>

    Actually, the result must be :
    ==> Postponed

    Hopefully could somebody give some help/advices...


    Code:
    #include <stdio.h>
    
    const char *strs[11] = {
      NULL, "Delivered",  "Postponed", NULL,
      NULL, "In progress", NULL,       NULL,
      "Cancel", NULL, "Done"
    };
    
    char *
    func (int c)
    {
            static char s[64];
    
            if (c < 0 || c > 10)
                    strcpy(s, strs[c]);
            if (!s)
                    strcpy(s, "Unknown");
    
            return s;}
    
    main()
    {
            printf ("==> %s\n", func('2'));
    }

    TQ
    BH

  2. #2
    Join Date
    Apr 2002
    Location
    Germany
    Posts
    228
    You cannot return a pointer to a string that isn't visible outside your function. You are creating the string on the stack.
    Possible solutions (in no particular order):

    1. You could create the string on the heap with malloc and return the pointer to that, but don't forget to free the memory when you're done with it!

    2. Or you can return a pointer to your const array without any strcpy. But you'll obviously need a const char* return value then which might be unsuitable for your needs.

    3. Or you can pass your function a pointer to a string buffer that will be filled by your function (windows api style). But beware of buffer overflows!

  3. #3
    Join Date
    Dec 2003
    Location
    chennai
    Posts
    27

    Re: C Program: FUNCTION failed to return ARRAY values

    in the main function ur calling func('2')
    instead u just use in main function use func(2)

    try this








    Originally posted by bh_perl
    Hi...

    I have some problem regards to my C programming ...(refer as below )

    As your info, my program did not return any numbers of the ARRAY VALUE's...it's just return NULL, as example :
    ==>

    Actually, the result must be :
    ==> Postponed

    Hopefully could somebody give some help/advices...


    Code:
    #include <stdio.h>
    
    const char *strs[11] = {
      NULL, "Delivered",  "Postponed", NULL,
      NULL, "In progress", NULL,       NULL,
      "Cancel", NULL, "Done"
    };
    
    char *
    func (int c)
    {
            static char s[64];
    
            if (c < 0 || c > 10)
                    strcpy(s, strs[c]);
            if (!s)
                    strcpy(s, "Unknown");
    
            return s;}
    
    main()
    {
            printf ("==> %s\n", func('2'));
    }

    TQ
    BH

  4. #4
    Join Date
    Apr 2002
    Location
    Germany
    Posts
    228

    Re: C Program: FUNCTION failed to return ARRAY values

    Originally posted by sendtovasu
    in the main function ur calling func('2')
    instead u just use in main function use func(2)

    try this
    maybe I really should read posts better

    yes, i didn't look there properly the first time, actually passing 50 (ASCII code of '2') instead of 2 there.
    I didn't look for s being static either before but you still cannot access it's data outside the function scope:
    Static variables are created and initialized once, on the first call to the function. Subsequent calls to the function do not recreate or re-initialize the static variable. When the function terminates, the variable still exists on the _DATA segment, but cannot be accessed by outside functions.
    Additionally the line
    if (!s)strcpy(s, "Unknown");
    is very dangerous as s is undefined when c<0 or c>10 and NOT neccesarily NULL.
    edit: or well, static variables get initialized with 0 so this should work again, *sigh*, what did I write above?
    Last edited by Apel; 12-10-03 at 09:07.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •