简体   繁体   中英

C - Copying Array of Strings into a Struct

I've been banging my head against the wall on this for a couple hours now.

I have a struct defined as follows:

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

I am attempting to copy a passed-in array of strings into the args field within the above struct. This happens in the method below:

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

    ...

When I attempt to access this node's args value at a later time outside of the method, it spits out a value equal to whatever is in the passed-in args array at that moment; ie, the values aren't actually being copied, but rather the addresses are.

I feel like this is simple but it is frustrating me. Any tips on how this can be remedied are super appreciated.

I attempt to access this node's args value at a later time outside of the method

For doing that you should've returned pointer to struct historyNode wherever you are trying to access the args value. Here is an example for that:

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

OUTPUT: hello World

In 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

I hope this could help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM