C-将字符串数组复制到结构中

时间:2018-09-15 23:42:11

标签: c pointers

我已经将头撞到墙上了几个小时。

我的结构定义如下:

typedef struct historyNode {
    int pos;
    char cmd[MAXLINE];
    char* args[(MAXLINE / 2) + 1];
    struct historyNode* next;
} historyNode_t;

我正在尝试将传入的字符串数组复制到上述结构内的args字段中。发生这种情况的方法如下:

void addToHistory(history_t* history, char* args[(MAXLINE / 2) + 1]) {
    historyNode_t* node = malloc(sizeof(historyNode_t));

    ...

    int index = 0;
    while (args[index] != NULL) {
        node->args[index] = args[index];

    ...

当我稍后尝试在该方法之外访问此节点的args值时,它吐出的值等于当时传入的args数组中的值;也就是说,实际上并没有复制值,而是复制了地址。

我觉得这很简单,但这让我感到沮丧。对此方法的任何提示都将受到高度赞赏。

2 个答案:

答案 0 :(得分:0)

  

我稍后尝试在方法之外访问此节点的args值

为此,无论您尝试访问struct historyNode值的何处,都应该返回指向args的指针。这是一个示例:

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

 struct node
 {
     char *a[2];
 };

 struct node *fun()
 {
     char *c[]={"hello","World"};
     struct node *ptr= malloc(sizeof(struct node));
     ptr->a[0]=c[0];
     ptr->a[1]=c[1];
     return ptr;
 }

 int main()
 {
     struct node *ptr=fun();
     printf("%s %s\n",ptr->a[0],ptr->a[1]);
     return 0;
 }

输出: hello World

答案 1 :(得分:0)

在C,C ++中,

char   charA[10];        // Array of char (i.e., string) up to 9+1 byte
                         // 10 bytes of memory is reserved

char  *string;           // Pointer to a null-terminated string
                         // Memory for 1 pointer (4 or 8 bytes) are reserved
                         // Need to allocate arbitrary bytes of memory
                         // Up to programmer to interpret the memory structure
                         // E.g.,
                         //   As array of pointers to string
                         //   A long string 
                         //   etc.
                         // This pointer could be passed to other functions
                         // and content at that pointed address could be changed

//char  *strings[];      // Cannot declare pointer to unknown length of array
                         // Use char** as below

char **ptrs2Strings;     // Pointer to pointer to char
                         // Memory for 1 pointer (4 or 8 bytes) are reserved
                         // Need to allocate arbitrary bytes of memory 
                         // Up to programmer to interpret the memory structure
                         // E.g.,
                         //   As array of pointers to string
                         //   A long string 
                         //   etc.
                         // The pointer to pointer could be passed to other 
                         // functions. The content at that pointed address is
                         // only an address to an user-allocated memory.
                         // These functions could change this second address as
                         // well as content of the memory pointed by the second
                         // address.

char  *charvar[10];      // array of 10 char pointers
                         // Memory for 10 pointers (40 or 80 bytes) are reserved
                         // Programmer could  allocate arbitrary bytes of memory 
                         // for each char pointer

char   stringA[10][256]; // array of 10 strings, and each string could store 
                         //   up to 255+1 bytes

希望这对您有所帮助。