0
#include<stdio.h>
int main(){
 char *p[2];
 char *q[2];
 char *a = "Hello";
 char *b = "world";
 p[0] = a;
 p[1] = b;
 *q = *p;
 printf("\n %s \n",q[0]);
 printf("\n %s \n",q[1]);
 return 0;
}

I wanted to store the pointers stored in the p to q. when tried to print q[1], i'm getting a junk value. But q[0] prints "Hello" correctly.

4 Answers 4

2

*q = *p is the same thing as q[0] = p[0]. That's why printing q[0] works but printing q[1] doesn't. If you want to copy all the pointers you'll need either a for loop or memcpy().

memcpy(q, p, sizeof(p))
Sign up to request clarification or add additional context in comments.

10 Comments

But I want to write the *p[2] into a file write(fd,p,size) is not possible;
sizeof(p) will return the size of 2character pointers which will be 8. If you wish to write both the strings to a file, you will have write them in a loop.
@Angus: Why would you want to write a pointer to a file? That's a serious mistake, because the pointer is meaningless to the file, and to future programs (including this program). Which book are you reading?
@modifiablelvalue I understand my statement is specific to a particular platform where sizeof of a character pointer is 4 which can't be guaranteed. I don't understand why you say my comment was wrong. Your point of sizeof(char *) being more than 4 is valid, but is very specific to certain systems.
@Ganesh: You don't even have to sit at a different computer, or a different OS to get different pointer sizes. In the past, sizeof (char *) has also been two, and can quite easily be all over again if you download one of those old compilers. At the current point in time, most 64-bit implementations will use 64-bit pointers. A 32-bit implementation will also run on 64-bit hardware. In which day and age are you programming where sizeof (char *) isn't more commonly 8?
|
0

The problem here like Kevin mentioned is that what you have is an array of pointers... so in order to accomplish what you want you need to memcpy the pointers over into the other array. This is because (these are example values mind you :)

p[0] = 0xaaa
p[1] = 0xbbb

*q == 0xaaa

so

q[0] = 0xaaa

q[1] is junk

So you are not copying the array, only a single value :) Hope this helps!

Comments

0

I understand what you want to do. You think you use " *q = *p; " to assign all pointers in p to q. But did you know that just assign p[0] to q[0], p and q are not pointers. They are just a array, you can't use this syntax to copy a whole array to another, but you can use memcpy or assign its elements one by one.

Comments

0

If I understand what you're trying to accomplish correctly, declare q as a double pointer **q, and assign it like this: q = p.

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.