copy array of strings in single pointer

Discussion in 'C Programming' started by raghujindia@gmail.com, Aug 24, 2012.

  1. Guest

    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.
     
    , Aug 24, 2012
    #1
    1. Advertising

  2. Mark Bluemel Guest

    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
     
    Mark Bluemel, Aug 24, 2012
    #2
    1. Advertising

  3. Guest

    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)

    >
    > > 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.
     
    , Aug 24, 2012
    #3
  4. Les Cargill Guest

    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;
    }

    --
    Les Cargill
     
    Les Cargill, Aug 24, 2012
    #4
  5. <> schrieb im Newsbeitrag
    news:...

    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?
     
    Heinrich Wolf, Aug 24, 2012
    #5
  6. Guest

    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.
     
    , Aug 24, 2012
    #6
  7. James Kuyper Guest

    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
     
    James Kuyper, Aug 24, 2012
    #7
  8. Guest

    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) 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.
     
    , Aug 24, 2012
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Venkat
    Replies:
    4
    Views:
    975
    Venkat
    Dec 5, 2003
  2. Alex
    Replies:
    2
    Views:
    1,233
  3. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    767
    Malcolm
    Jun 24, 2006
  4. Replies:
    26
    Views:
    2,119
    Roland Pibinger
    Sep 1, 2006
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    457
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page