copy array of strings in single pointer

R

raghujindia

I have double pointer say char **id having the array of strings in ascii like id[0]= "3132" id[1]= "3635" ..etc so when i do printf %s of id[0] and id[1] i get 12 and 65 as output.
I need to store these output values in a char *p pointer so that at later point i can get these values using index p[0],p[1] .. etc.
I have tried for(i=0;i<size;i++)sprintf(p+i,"%s",id) but it did not help. Any other ideas. Thanks for the help.
 
M

Mark Bluemel

On 24/08/2012 11:54, (e-mail address removed) wrote:
[Aside - gmail's getting worse isn't it?]
I have double pointer say char **id having the array of strings

I think you need to learn the terminology, so you can ask your
questions clearly. "double pointer" is most likely to be read as
a pointer to a double. That's not what you have - you have a pointer
to a pointer to char, which in this case seems to be a pointer to
the first of an array of pointers, each of which points to the first
character of a C string.
in ascii like id[0]= "3132" id[1]= "3635" ..etc so when i do
printf %s of id[0] and id[1] i get 12 and 65 as output.

"3132" isn't what you mean, either
I need to store these output values in a char *p pointer

Well you can't. All you can store in a char * is one address
which is the address of a char.
so that at later point i can get these values using index
p[0],p[1] .. etc.

If p is char *, then p[0] is a char.
I have tried for(i=0;i<size;i++)sprintf(p+i,"%s",id)
but it did not help.


I'm not surprised.
Any other ideas.

Tell us more clearly what you are trying to achieve, and
show us real compilable code.

http://www.catb.org/esr/faqs/smart-questions.html
 
R

raghujindia

On 24/08/2012 11:54, wrote:

[Aside - gmail's getting worse isn't it?]


I have double pointer say char **id having the array of strings



I think you need to learn the terminology, so you can ask your

questions clearly. "double pointer" is most likely to be read as

a pointer to a double. That's not what you have - you have a pointer

to a pointer to char, which in this case seems to be a pointer to

the first of an array of pointers, each of which points to the first

character of a C string.


in ascii like id[0]= "3132" id[1]= "3635" ..etc so when i do
printf %s of id[0] and id[1] i get 12 and 65 as output.



"3132" isn't what you mean, either


I need to store these output values in a char *p pointer



Well you can't. All you can store in a char * is one address

which is the address of a char.


so that at later point i can get these values using index
p[0],p[1] .. etc.



If p is char *, then p[0] is a char.


I have tried for(i=0;i<size;i++)sprintf(p+i,"%s",id)

but it did not help.



I'm not surprised.


Any other ideas.



Tell us more clearly what you are trying to achieve, and

show us real compilable code.



http://www.catb.org/esr/faqs/smart-questions.html



I'm sorry Maybe I did not frame my query correctly. There are ascii stringsstored in a memory and that memory address is stored in a pointer to a double. I need to store these ascii string values in my memory location.
Say A = "string1" , B = "string2" where A and B are memory addresses. These A,B are stored in a pointer to double i.e say if char **dp is the pointer to a double, then dp[0] contains A and dp[1] contains B . if char *ptris a pointer pointing my memory location, I need to copy those ascii strings in ptr.
Hope I'm clear. thanks for the help.
 
L

Les Cargill

I have double pointer say char **id having the array of strings in
ascii like id[0]= "3132" id[1]= "3635" ..etc so when i do printf %s
of id[0] and id[1] i get 12 and 65 as output. I need to store these
output values in a char *p pointer so that at later point i can get
these values using index p[0],p[1] .. etc. I have tried
for(i=0;i<size;i++)sprintf(p+i,"%s",id) but it did not help. Any
other ideas. Thanks for the help.


I have a little trouble understanding you, so I will guess at what you
want. This ain't tested code. It's whiteboard code.

// This is your double pointer thing using array notation
// If you try it, try double pointer notation - should work
// fine.

char *alph[] = {
"One",
"Two",
"Three"
};

char bob[4096];

char *p = bob.

int main(void)
{
const int lim = sizeof(alph)/sizeof(alph[0]);
// alph[0] is still a pointer
assert(lim==3); // sanity check - comment out later.

memset(bob,0,sizeof(bob));

for (k=0;k<lim;k++)
{
strcat(p,alph[k]);
p += strlen(p);
p++;
}
// \0 means a null character.
// At this point, bob is "One\0Two\0Three\0\0....\0"

p = bob[0];

k = 0;
while (*p)
{
printf("%d: %s\n",k,p);
p+= strlen(p);
p++; // past the null...
k++;
}

return 0;
}
 
H

Heinrich Wolf

I'm sorry Maybe I did not frame my query correctly. There are ascii strings
stored in a memory and that memory address is stored in a pointer to a
double. I need to store these ascii string values in my memory location.
Say A = "string1" , B = "string2" where A and B are memory addresses. These
A,B are stored in a pointer to double i.e say if char **dp is the pointer to
a double, then dp[0] contains A and dp[1] contains B . if char *ptr is a
pointer pointing my memory location, I need to copy those ascii strings in
ptr.
Hope I'm clear. thanks for the help.
 
R

raghujindia

<> schrieb im Newsbeitrag

...



I'm sorry Maybe I did not frame my query correctly. There are ascii strings

stored in a memory and that memory address is stored in a pointer to a

double. I need to store these ascii string values in my memory location.

Say A = "string1" , B = "string2" where A and B are memory addresses. These

A,B are stored in a pointer to double i.e say if char **dp is the pointer to

a double, then dp[0] contains A and dp[1] contains B . if char *ptr is a

pointer pointing my memory location, I need to copy those ascii strings in

ptr.

Hope I'm clear. thanks for the help.

------------------------------------------------------------------

I still can't believe that you mean "pointer to a double". double is a

numeric floating point type. Do you mean pointer to a pair of strings?

Oops.. my bad..
I meant its a pointer to pointer.
 
J

James Kuyper

On 08/24/2012 07:31 AM, (e-mail address removed) wrote:
....
I'm sorry Maybe I did not frame my query correctly. There are ascii strings stored in a memory and that memory address is stored in a pointer to a double. I need to store these ascii string values in my memory location.
Say A = "string1" , B = "string2" where A and B are memory addresses. These A,B are stored in a pointer to double i.e say if char **dp is the pointer to a double, then dp[0] contains A and dp[1] contains B . if char *ptr is a pointer pointing my memory location, I need to copy those ascii strings in ptr.
Hope I'm clear. thanks for the help.

Since the only things that you describe involving doubles are your
pointers to double, I strongly suspect that you should not be using such
pointers for this purpose. Wherever you currently have double* (a
pointer to double), try replacing it with char** (a pointer to a pointer
to char). Without a clearer statement of your problem, it's hard to give
more specific advice, but that at least should be a good starting point.

The best way to make it clear what you want is to provide a code sample.
Preferably a complete working program, as small as possible while
demonstrating what you're talking about. If your problem is that you
can't get it to work, a complete compilable program is second best. If
your problem is that you can't get it to compile, a complete program
that doesn't compiler and the associated error messages would be
helpful. If you can't even figure out how to start, you at least need to
provide a better description of what you're trying to do.

It's clear that you don't know how to correctly use words describing C
language constructs - it might be better to describe what you want to do
in terms of what a user of your program would see, rather than in terms
of how your program does it.
 
R

raghujindia

wrote:
I have double pointer say char **id having the array of strings in
ascii like id[0]= "3132" id[1]= "3635" ..etc so when i do printf %s
of id[0] and id[1] i get 12 and 65 as output. I need to store these
output values in a char *p pointer so that at later point i can get
these values using index p[0],p[1] .. etc. I have tried
for(i=0;i<size;i++)sprintf(p+i,"%s",id) but it did not help. Any

other ideas. Thanks for the help.



I have a little trouble understanding you, so I will guess at what you

want. This ain't tested code. It's whiteboard code.



// This is your double pointer thing using array notation

// If you try it, try double pointer notation - should work

// fine.



char *alph[] = {

"One",

"Two",

"Three"

};



char bob[4096];



char *p = bob.



int main(void)

{

const int lim = sizeof(alph)/sizeof(alph[0]);

// alph[0] is still a pointer

assert(lim==3); // sanity check - comment out later.



memset(bob,0,sizeof(bob));



for (k=0;k<lim;k++)

{

strcat(p,alph[k]);

p += strlen(p);

p++;

}

// \0 means a null character.

// At this point, bob is "One\0Two\0Three\0\0....\0"



p = bob[0];



k = 0;

while (*p)

{

printf("%d: %s\n",k,p);

p+= strlen(p);

p++; // past the null...

k++;

}



return 0;

}


thanks a lot.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top