3

I want this program to return an array, and then a variable from main to get this array from the function, and be usable in a part of code that has to verify a string against these values from the initial variable. But the variable doesn't get the returned array. I've seen some posts saying that you cannot return arrays in C, but I've also seen other post where this was possible.

But I want to know what can I do to catch this returned value.

int getUsers()
{
    char userVect[5][25];
    char user[24];
    int i = 0, j = 0, k;

    FILE  *usernames;
    usernames = fopen("usernames.cfg", "r");
    if (usernames == NULL){
        perror("usernames - err");
        return(-1);
    }

    while(fgets(user, sizeof(user), usernames) !=NULL){
        strcpy(userVect[j], user);
        j++;
    }

    fclose(usernames);
    for(k=0; k<j; k++)
        printf("Usernames are:  %s\n", userVect[k]);

    return userVect;
}

int main()
{
    printf("\nWelcome to Sys v1.24. To start off, please insert your command. \n");
    char *users;
    users = getUsers();
    printf("%c\n", users);

    return 0;
}
2
  • 1
    You are returning stack variable (userVect) from getUsers() function. As soon as, the getUsers() function call is returned, 'userVect' stack storage variable is not valid to be used. Either you can declare userVect in main() and pass it to getUsers(), yes, getUsers() will be able to update its content Or you can dynamically allocated pointer in getUsers() and return it. Commented Nov 1, 2015 at 6:43
  • Then main reason why this is not working is that function getUser() is returning int. This will give output correctly on a 32 bit system where sizeof(int)==sizeof(ptr). But on 64 bit systems size of int and size of ptr are different, hence the issue. Also userVect[5][25], need to be made global or declared inside main and passed to function getUsers(); Commented Nov 1, 2015 at 8:20

4 Answers 4

7

1. You want to return a string and your function has type int??

I see you return a 2-d array, then your function should be of type char ** -

char **getUser(void)

2. You return local variable (userVect) from your function. You cannot access it outside function block .

You should do this in you function getUser-

char **userVect;
userVect=malloc(5*sizeof(char **));              //allocate memory  
for(int i=0;i<5;i++)
   userVect[i]=malloc(25*sizeof(char));   

And in main do this -

char **user;
...
user=getUsers();                 // call function 

3. And to printuser in main -

for(int i=0;i<5;i++)
    printf("%s\n", user[i]);          //this is wrong in your code- printf("%c\n", users);

And then free it in main at then end-

for(int i=0;i<5;i++)
    free(user[i]);
free(user);
Sign up to request clarification or add additional context in comments.

8 Comments

For a small dataset like this one a stack should be sufficient, I would go the heap route.
@sdayal I don't see a problem in using dynamic allocation . But if you want pass array to function from main , if you don't want to use dynamic allocation .
Dynamic allocation poses more risk in maintaining. For small data sets why would you want to change his code to heap?
@sdayal That's opinion based and there wont be risk if you are aware what you are doing . And small dataset is no reason for not using dynamic allocation IMHO . But as I said its on your opinion :-)
@sdayal I wouldn't have used global array rather avoiding is better , for such things its completely unnecessary . If you dont want to use dynamic allocation then pass array to function from main that's better than using global vairables.
|
2

You are returning a stack variable. It won't be valid in main(). You should either allocate it on the heap (using malloc) or make it a global variable (in which case there is no need to return it at all).

Comments

0
char ** getUsers(){
    char **userVect;
char user[24];
int i = 0, j = 0, k;

userVect=malloc(5*sizeof(char **));              //allocate memory  

for(int i=0;i<5;i++)
    userVect[i]=malloc(25*sizeof(char)); 

FILE  *usernames;
usernames = fopen("usernames.cfg", "r");
if (usernames == NULL){
    perror("usernames - err");
    return(-1);
}

    while(fgets(user, sizeof(user), usernames) !=NULL){
    strcpy(userVect[j], user);
    j++;
}

fclose(usernames);
for(k=0; k<j; k++)
    printf("Usernames are:  %s\n", userVect[k]);

return userVect;`

`int main(){ printf("\nWelcome to Sys v1.24. To start off, please insert your command. \n"); char **users;

users = getUsers();

for(int i=0;i<5;i++) printf("%s\n", user[i]);

return 0; }`

Comments

-6

The function getUser() should return a char*

    #include<stdio.h>
    char userVect[5][25];
    char* getUsers(){ 
        char user[24];
        int i = 0, j = 0, k;

        FILE  *usernames;
        usernames = fopen("usernames.cfg", "r");
        if (usernames == NULL){
            perror("usernames - err");
            return(-1);
        }

        while(fgets(user, sizeof(user), usernames) !=NULL){
            strcpy(userVect[j], user);
            j++;
        }

        fclose(usernames);
        for(k=0; k<j; k++)
            printf("Usernames are:  %s\n", userVect[k]);

        return userVect;
    }

    int main(){
        printf("\nWelcome to Sys v1.24. To start off, please insert your command. \n");
        char *users;
        users = getUsers();
        printf("%c\n", users);

        return 0;
    }

you can also declare userVect[5][25] in main() and pass it as an argument to getUsers() function.

You can also get the data in heap (via malloc), but for small dataset like this I wouldn't recommend.

Then main reason why this is not working is that function getUser() was returning int. This will give output correctly on a 32 bit system where sizeof(int)==sizeof(ptr). But on 64 bit systems size of int and size of ptr are different.

You also need to consider point 3 from @ameyCU answer regarding printing the output.

3 Comments

If this works it is pure luck. Returning a pointer to the stack is extremely bad practice in C.
userVect is stack variable and being returned from function getUsers(). This is wrong.
There are lots of other issues with this code as well which the author will need to take a look at. Making the array global will work. There are also 5 compiler warning that will need to be looked into. I just pointed the main issue with the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.