2012-01-24 181 views
1

我真的很难得到这段代码的工作。我试图通过引用传递一个数组,以便在该函数中对其进行修改。然后我需要将这些修改回传给原来的呼叫者功能。传递数组参考函数C

我在这里搜索了类似的问题,但找不到任何可以像我想要做的方式成功运行。

这是我的代码,我非常感谢任何帮助。非常感谢:

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

#define SIZE_OF_VALUES 5 
#define SIZE_OF_STRING 100 

void set_values(char **values); 

void set_values(char **values) 
{ 
    *values = malloc(sizeof(char)*SIZE_OF_VALUES); 
    for (int i = 0; i < (sizeof(char)*SIZE_OF_VALUES); i++) { 
     values[i] = malloc(sizeof(char)*SIZE_OF_STRING); 
     values[i] = "Hello"; 
     //puts(values[i]); //It works fine here. 
    } 
} 

int main (int argc, const char * argv[]) 
{ 
    char *values; 
    set_values(&values); 

    for (int i = 0; i < (sizeof(char)*SIZE_OF_VALUES); i++) { 
     puts(values[i]); //It does not work! 
    } 

    return 0; 
} 
+0

你正在尝试创建一个数组数组,所以“values”应该是一个指针指针(char **值)。 –

+0

感谢您的回复,我已更改为(char **值)并删除了(&)引用,但代码仍然不起作用 –

回答

3

有几个问题与您的代码:

  1. 你应该有三个层次的三分球 - void set_values(char ***values),读它作为“参考(第一*)至char*数组(第二*)(第三*)”

  2. *values每个元素应该是一个指针(char*)不char,所以你需要:

    *values = malloc(sizeof(char*)*SIZE_OF_VALUES); 
    
  3. 您正在泄漏内存,第一mallocing然后分配文字,另外不提领values,你需要或者:

    (*values)[i] = "Hello"; 
    

    (*values)[i] = strdup("Hello"); // you will have to free it later 
    

    (*values)[i] = malloc(sizeof(char)*SIZE_OF_STRING); // you will have to free this as well 
    strcpy((*values)[i], "Hello"); 
    
  4. 在您的main,您应该声明char **values;,因为它是指向char*(字符串/数组)的数组的指针。

  5. 在你循环中,你错误地将索引乘以sizeof,索引被计算在不是以字节为单位的元素中。因此,您需要:

    for (int i = 0; i < SIZE_OF_VALUES; i++) 
    
  6. 不要忘记在最后释放内存。

+0

哇,非常感谢您的快速回复和对问题的出色解释。我接受了你的答案。非常感谢你! –

0

使用char ***类型,请您set_values函数的参数:

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

#define SIZE_OF_VALUES 5 

void set_values(char ***values) 
{ 
    *values = malloc(sizeof (char *) * SIZE_OF_VALUES); 
    for (int i = 0; i < SIZE_OF_VALUES; i++) { 
     (*values)[i] = "Hello"; 
    } 
} 

int main (int argc, char *argv[]) 
{ 
    char **values; 
    set_values(&values); 

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

    return 0; 
} 

当然,你必须检查malloc返回值的主要出口之前free分配的内存。

-3

你在这一行额外*

*values = malloc(sizeof(char)*SIZE_OF_VALUES); 

这应该是:

values = malloc(sizeof(char)*SIZE_OF_VALUES); 

你也有在主相当大的问题,char* valueschar** values,通过你的char* values引用(set_values(&values);)可能会导致我怀疑的分段错误。

对于影响外阵列,它已经受到影响传递给函数时,因为您只复制指向同一个地方一个指针,以便修改将影响相同的内存块。

+1

仅通过引用传递仅在C++中受支持。由于问题标记为“C”,因此他需要三个级别的指针。 –

0

char *是char的一维数组。但是你想让你的代码在set_values中将值设置为二维数组。 为了使这项工作,定义:

char **values;

函数set_values为void set_values(char ***values)

分配指针为字符数组为: *values = malloc(sizeof(char*)*SIZE_OF_VALUES);

除此之外你的循环是有点奇怪:

for (int i = 0; i < (sizeof(char)*SIZE_OF_VALUES); i++) { 

for (int i = 0; i < SIZE_OF_VALUES; i++) { 

,最终被替换,如果要复制一个字符串到你现在分配的数组总共使用

strncpy((*values)[i], "Hello", SIZE_OF_STRING-1); 
(*values)[i][SIZE_OF_STRING-1] = '\0'; 

这样:

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


#define SIZE_OF_VALUES 5 
#define SIZE_OF_STRING 100 

void set_values(char ***values); 

void set_values(char ***values) 
{ 
    const char * content = "Hello"; 
    *values = malloc(sizeof(char*)*SIZE_OF_VALUES); 
    for (int i = 0; i < SIZE_OF_VALUES; i++) { 
     (*values)[i] = malloc(sizeof(char)*SIZE_OF_STRING); 
     strncpy((*values)[i], content, SIZE_OF_STRING-1); 
     (*values)[i][SIZE_OF_STRING-1] = '\0'; 
     if(strlen(content) >= SIZE_OF_STRING){ 
      fprintf(stderr,"Warning content string did not fit into buffer!\n"); 
     } 
    } 
} 

int main (int argc, const char * argv[]) 
{ 
    char **values; 
    set_values(&values); 

    for (int i = 0; i < SIZE_OF_VALUES; i++) { 
     printf("%s\n", values[i]); 
    } 

    for (int i = 0; i < SIZE_OF_VALUES; i++) { 
     free(values[i]); 
    } 
    free(values); 

    return 0; 
} 
0

下面是解。

void set_values(char ***values) 
{ 
    int i; 
    char ** val; 

    val = *values = (char**)malloc(sizeof(char*)*SIZE_OF_VALUES); 
    for (i = 0; i < (sizeof(char)*SIZE_OF_VALUES); i++) 
     val[i] = "Hello"; 
} 

int main (int argc, const char * argv[]) 
{ 
    char **values; 
    int i; 
    set_values(&values); 

    for (i = 0; i < (sizeof(char)*SIZE_OF_VALUES); i++) 
     puts(values[i]); 

    return 0; 
} 
+1

只需发布固定代码教OP什么都没有。对我而言,这是毫无价值的答案。 – Krizz