0

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

1
  • This example has a lot of flaws. Examples: not casting the result of malloc, assigning a (char *) to both (a) and (*a). Can you post real code? What is it that you are trying to do? If you are having problems compiling - start with the compiler errors. If you are having problems trying to get it to run - please post code that compiles and start with a good debugger and smaller arrays so that you can see what is going on at each step. Commented Apr 27, 2011 at 17:37

6 Answers 6

3
a = (char *) &r[13];             // point a to value at r[13]

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 r is char** and so the type of r[13] is char*, and the type of &r[13] is char**, which you're assigning to a char*.

P.s., next time please also include the actual error you receive vs what you expected.

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

Comments

0

To assign a string, use strncpy. Don't copy the pointer value directly because you will free it twice later this way (among other problems).

3 Comments

So something like: strcpy(a, &code[100]); It still doesn't seem to store the correct value according the the printf
You don't need the extra &, because code[100] already points to the beginning of a string.
Safest is strncpy(a, code[100], 31); a[31] = '\0'; You should use a constant instead of 32 literally, of course.
0

&r[13] is not a pointer to char.

You just need to

r[13] = code[100]

I do agree with Frederik that you should be careful when free()-ing the allocated memory since you now have two pointers pointing at the same memory block. If you prefer to follows his advice, try the following:

strncpy(r[13], code[100], 8)

Comments

0

Rather than assigning a pointer to a memory address, you're going to want to copy the data at that memory address using strcpy.

Comments

0

With definition of char *a; the *a on the left-hand side of an assignment becomes an lvalue of type char. You can assign pointer values there as integers, as in square peg into a round hole, though it does not make much sense. To copy strings use str[nl]cpy(3).

Comments

0

I hate to ask a question during someone else's question... but shouldn't the char array setup/malloc calls be something more like this? Isn't he allocating too much with sizeof(**code)? And then... if it's for 8 characters... won't we want 9 to make room for '\0'?

char **code;                      // code is an array of 100 strings of length 8 characters
code = (char**) malloc (100*sizeof(char*));
for (i=0; i<100; i++) {
   code[i] = (char*)malloc(9*sizeof(char));
}

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.