簡體   English   中英

C 返回字符串數組

[英]C Returning string array

我希望這個程序返回一個數組,然后從 main 返回一個變量以從函數中獲取這個數組,並且可以在必須根據初始變量中的這些值驗證字符串的部分代碼中使用。 但是變量沒有得到返回的數組。 我看過一些帖子說你不能用 C 返回數組,但我也看過其他帖子,這是可能的。

但我想知道我能做些什么來捕捉這個返回的值。

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;
}

1.你想return一個字符串並且你的函數的類型是int ??

我看到你返回一個2-d數組,那么你的函數應該是char **類型 -

char **getUser(void)

2.從函數中返回局部變量 ( userVect )。 您不能在功能塊之外訪問它。

您應該在函數getUser執行此操作 -

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

main是這樣做 -

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

3.並在main打印user -

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

然后在mainfree它,然后結束 -

for(int i=0;i<5;i++)
    free(user[i]);
free(user);

您正在返回一個堆棧變量。 它在main()無效。 您應該在堆上分配它(使用malloc )或使其成為全局變量(在這種情況下根本不需要返回它)。

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("\\n歡迎使用 Sys v1.24。要開始,請插入您的命令。\\n"); 字符**用戶;

用戶 = getUsers();

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

返回0; }`

函數 getUser() 應該返回一個 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;
    }

您還可以在 main() 中聲明 userVect[5][25] 並將其作為參數傳遞給 getUsers() 函數。

您也可以在堆中獲取數據(通過 malloc),但對於像這樣的小數據集,我不推薦。

那么這不起作用的主要原因是函數 getUser() 正在返回 int。 這將在 32 位系統上正確輸出,其中 sizeof(int)==sizeof(ptr)。 但是在 64 位系統上 int 的大小和 ptr 的大小是不同的。

您還需要考慮@ameyCU 回答中關於打印輸出的第 3 點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM