Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > copy array of strings in single pointer

Reply
Thread Tools

copy array of strings in single pointer

 
 
raghujindia@gmail.com
Guest
Posts: n/a
 
      08-24-2012
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[i]) but it did not help. Any other ideas. Thanks for the help.
 
Reply With Quote
 
 
 
 
Mark Bluemel
Guest
Posts: n/a
 
      08-24-2012
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[i])
> 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


 
Reply With Quote
 
 
 
 
raghujindia@gmail.com
Guest
Posts: n/a
 
      08-24-2012
On Friday, August 24, 2012 4:42:43 PM UTC+5:30, Mark Bluemel wrote:
> 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[i])

>
> > 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.
 
Reply With Quote
 
Les Cargill
Guest
Posts: n/a
 
      08-24-2012
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[i]) 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;
}

--
Les Cargill
 
Reply With Quote
 
Heinrich Wolf
Guest
Posts: n/a
 
      08-24-2012

<> schrieb im Newsbeitrag
news:17cd55c9-b1c6-494d-b6fa-...

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?

 
Reply With Quote
 
raghujindia@gmail.com
Guest
Posts: n/a
 
      08-24-2012
On Friday, August 24, 2012 5:36:23 PM UTC+5:30, Heinrich Wolf wrote:
> <> 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.
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      08-24-2012
On 08/24/2012 07:31 AM, 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.
--
James Kuyper
 
Reply With Quote
 
raghujindia@gmail.com
Guest
Posts: n/a
 
      08-24-2012
On Friday, August 24, 2012 5:16:37 PM UTC+5:30, Les Cargill wrote:
> 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[i]) 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;
>
> }
>
>
>
> --
>
> Les Cargill


thanks a lot.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
Array of pointer and pointer of array erfan C Programming 6 01-28-2008 08:55 PM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
Array of pointer Vs Pointer to Array sangeetha C Programming 9 10-09-2004 07:01 PM
How do copy Strings from a single dimensional array to double dimensional array Venkat C++ 4 12-05-2003 09:23 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57