If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Delphi, C etc > C Program: FUNCTION failed to return ARRAY values

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-19-03, 00:30
bh_perl bh_perl is offline
Registered User
 
Join Date: Oct 2003
Posts: 3
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
Reply With Quote
  #2 (permalink)  
Old 12-08-03, 11:02
Apel Apel is offline
Registered User
 
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!
Reply With Quote
  #3 (permalink)  
Old 12-10-03, 07:13
sendtovasu sendtovasu is offline
Registered User
 
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








Quote:
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
Reply With Quote
  #4 (permalink)  
Old 12-10-03, 08:01
Apel Apel is offline
Registered User
 
Join Date: Apr 2002
Location: Germany
Posts: 228
Re: C Program: FUNCTION failed to return ARRAY values

Quote:
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:
Quote:
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 08:07.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On