0

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?

3
  • You should do memcpy. Commented Nov 23, 2013 at 5:20
  • You can't do ptr[0]->field1 = value; when mystruct has no member field1. Please show the real code! Commented Nov 23, 2013 at 5:33
  • sorry I forgot to erase that line; the code is correct now Commented Nov 23, 2013 at 5:36

3 Answers 3

1
#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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.