David Lemporo
David Lemporo

Reputation: 101

(C) Array of Structs and moving data to it

I am looking to do the following:

struct def:

struct mystruct {
     char cArr[500];
}

global:

    struct mystruct **ptr;
    int count = 0;

in main:

ptr = malloc(20*sizeof(struct test *));
for (int i = 0; i != 20 ; i++) {
    ptr[i] = malloc(sizeof(struct test));
}

in some function that is called 20 times:

char burp[500];
//put whatever I want in burp;
ptr[count]->cArr = burp //is this right? Or do I have to memcpy to it, and if so how?
count++;

So at the end I will sequentially fill in the array of mystruct with the chars that I want. I tried doing this with char** but had no luck; I am now wrapping it in a struct as it helps me visualize what is going on.

So I want a global array of char[500], where everytime a function is called it puts that char[500] into the index (that is either passed into the function or also global).

Any advice is appreciated; Ofc I will need to free at the end every index of the array as well.

Thanks!

edit:

so would something like:

memcpy(ptr[count]->cArr, burp, 500);

work then?

Upvotes: 0

Views: 143

Answers (3)

Adit Ya
Adit Ya

Reputation: 771

I guess all that you wanted to do is to store some text in your structure for later usage.

struct mystruct {
   char carr[500];
}

struct mystruct *ptr = NULL;
int count = 0;

main{

    ...
    ptr = malloc( 20 * sizeof(struct test) );
    //Function call func()
    ...
    //After performing work
    free( ptr );
}

//Some function
func() {
    char burp[500];
    // burp has some data fed
    memcpy( ptr[count]->carr, burp, 500 );
}

Upvotes: 0

Rafed Nole
Rafed Nole

Reputation: 122

#include <stdio.h>

#include <stdlib.h>

struct  mystruct

 {
    char *cArr; 

  // U were trying to assign array using = operator

  // Remember its not like in STL where u can perform deep copy of a vector    

};


 struct mystruct **ptr;


 int count = 0;


 int main()

{   int i;

    ptr = malloc(20*sizeof(struct mystruct *));

    for (i = 0; i != 20 ; i++) 

{
    ptr[i] = malloc(sizeof(struct mystruct));

}

   char burp[500]="No this is not correct boy.";

  //put whatever I want in burp;

  (*ptr+count)->cArr = burp ; 

   // Assigning pointer to a pointer , OK. Remember pointer != Array.


  //is this right? Or do I have to memcpy to it, and if so how?


  //count++; // Has no use in your code, enclose in a loop to then use it.


  printf("%s\n",(*ptr + count)->cArr); // This works , I think.


 }

For arrays i.e. char cArr[500],

If you want to use memcpy u can use it :

memcpy((*ptr+count)->cArr, burp, 500);

Strcpy also works :

strcpy((*ptr+count)->cArr, burp);

Two points are important :

  1. Assignment of pointers to pointers is allowed, but deep copy of array is not.

  2. **ptr is a double pointer.So, (*ptr + count ) or ptr[count] is a pointer to struct.

2nd point is not required for your answer.

Upvotes: 1

Tonmoy
Tonmoy

Reputation: 557

You can use strcpy to copy the string.

strcpy(ptr[count]->cArr,burp);

But strcpy terminates on null character. So, make sure your character string(i.e burp) is properly initialized.

Upvotes: 0

Related Questions