2015-04-07 132 views
-1

我是C/pointers /內存管理的新手,並且在爲我正在開發的項目實現一些函數時遇到問題。C:從另一個函數返回字符串

在我的builtins.c文件中,我有一個名爲printalias的函數,它被調用來打印存儲在我的程序中的所有別名和相應的值。最後,我想通過另一個名爲getal的函數來檢索別名。

int x_printalias(int nargs, char *args[]) { 
    int i = 0; 
    // Loop through, print names and values 
    for(i = 0; i< 100; i++) 
    { 
    if(alias_names[i][0]!='\0' && !alias_disabled[i]) 
    { 
     char * var = alias_names[i]; 
     char * val = alias_vals[i]; 
     fprintf(stderr,"%s = %s\n", var, val); 
    } 
    } 
    // This is where I want to retrieve the string from another function 
    char * hello = "brett"; 
    hello = getal(hello); 
    fprintf(stderr,"Got alias for brett --> %s",hello); 
    return 0; 
} 

getal功能在我shellParser.c文件存在,並且看起來是這樣的,通常執行相同的循環,當它發現返回:

const char * getal(int nargs, char *args[]) 
{ 
    fprintf(stderr,"\nRetrieving alias...\n"); 
    int i = 0; 
    fprintf(stderr, "check1\n"); 

    fprintf(stderr,"Got args[0]: %s\n", args[0]); 

    while (alias_names[i][0]!='\0' && i < MAX_ALIAS_LENGTH) // Find empty slot in variables array 
    { 
    fprintf(stderr, "check2\n"); 

    fprintf(stderr,"I is currently %i and current varible in slot is %s\n",i,alias_names[i]); 
    //strncpy(hello, variables[i], MAX_VAR_LENGTH); // Variable at current slot 
    if(strcmp(alias_names[i], args[0]) == 0) // If we have an entry, need to overwrite it 
    { 
     fprintf(stderr,"Found alias %s = %s at spot %i\n",args[0],alias_vals[i], i); // Not at end if here 
     return alias_vals[i]; 
    } 
    i++; 
    } 
    fprintf(stderr, "check3\n"); 

    // Elided.... 

    return '\0'; 
} 

在我printalias函數的結尾,我要測試這個getal函數是通過在硬編碼字符串「brett」上調用它來工作的。但是,當我從命令行調用我的printalias function時,它會將其輸入到「Check 1」打印語句中,然後退出而沒有錯誤或返回值。

我認爲這與我的內存管理或指針變量聲明不正確有關。任何人都可以發現我在這裏做錯的事情(或許多事情)嗎?

+3

您調用'getal'功能與類型'字符*'和你的這個函數定義的一個參數說它需要兩個'int'和'char * []'類型的參數。 –

+0

如果你想在'getal'中使用可變參數,你將需要'#include '並聲明'const char * getal(int,...)'。省略號告訴編譯器該函數接受可變參數。使用'va_start()'來初始化你想要的'變量',比如'args'。並使用'va_end()'清理已用內存。 – alvits

+0

[從C中的函數返回字符串]的可能重複(http://stackoverflow.com/questions/9378998/returning-a-string-from-a-function-in-c) – Mgetz

回答

0

您必須聲明參數列表才能呼叫getal,並用這些 列表呼叫。返回值的 和指針的getal必須const char*

//.... 
    // This is where I want to retrieve the string from another function 
     char * hello[] = {"brett"}; // this list argument for getal function 
     const char *strGetal; 
     strGetal = getal(1,hello); 
     fprintf(stderr,"Got alias for brett --> %s",strGetal); 
     return 0; 
    } 
-1

例子:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 


char** get_all(int argc, char **argv) 
{ 
    char *value; 
    char **values = NULL; 
    int i; 

    values = (char**) malloc(sizeof (char) * argc); 
    if (values == NULL) { 
     perror("malloc"); 
     return NULL; 
    } 


    for (i = 0; i < argc; i++, argv++) { 
     value = strchr(*argv, ':'); 
     values[i] = (value + 1); 
    } 

    return values; 
} 

int main() 
{ 
    char *args[] = {"key:a", "key:b", "key:c"}; 
    char **values; 
    int i; 

    values = get_all(3, args); 

    for (i = 0; i < 3; i++) { 
     puts(values[i]); 
    } 

    return 0; 
}