I’m having some issues with storing an address of a specific value in an array of strings to a pointer and printing it out. Please excuse the bad variable names, they are just for the example.
char **code; // code is an array of 100 strings of length 8 characters
code = malloc (100*sizeof(*code));
for (i=0; i<100; i++) {
code[i]=malloc(8*sizeof(**code));
}
char **r; // r is an array of 16 strings of 32 characters
r = malloc (16*sizeof(*r));
for (i = 0; i < 16; i++)
r[i] = malloc(32*sizeof(**r));
char *a; // a is a pointer to a string
a = (char *) &r[13]; // point a to value at r[13]
*a = (char *)&code[100]; // trying to assign the value of r[13] to the address of code[100]
printf("Code 100 Add: %p a Val: %i\n", &code[100], *sp); // trying to check values.
I’m trying to assign the value of a (which points to r[13], so assign value of r[13]) to the value of the Address of the string at code[100]. Is even a string of 32 characters the best way to do this?
Appreciate any help,
Gareth
Turn on your compiler warnings, and pay attention to what the compiler tell you when you remove this cast. You shouldn’t need any casts in this code.
The type of
rischar**and so the type ofr[13]ischar*, and the type of&r[13]ischar**, which you’re assigning to achar*.P.s., next time please also include the actual error you receive vs what you expected.