Thread: convert char

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    77

    convert char

    hi all,
    I want ack about converting more like this element of array to one string...
    array[num]
    for example, I've similar of this arrays
    array[1]=1;
    array[2]=2;
    array[3]=3;
    array[4]=4;
    and I did need value from arrar[1-4] per one printf.
    currently i use it
    printf("%s",array[num], (num=3) ? '\n' : '.');
    So, I want decrease quantity of printf syscalls and print either string per one printf.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    so use sprintf to buffer first
    but printf is buffered output that outputs only the line when \n is sent (or fflush(stdout) is called)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    77
    Quote Originally Posted by vart View Post
    so use sprintf to buffer first
    but printf is buffered output that outputs only the line when \n is sent (or fflush(stdout) is called)
    Ok, then suppose to me, please how to do it most good. Me need four values separate of point. What's way will be of most good? I can do int by 'switch' or forest of 'if', but I think what that not quite good.
    Code:
    for (num=0;num<4; num++) {
    str += sprtinf(arr[num], sizeof(arr[num]));
    str += sprtinf(".");
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can put more than one thing in a format string, you know.
    Code:
    printf("%s. %s. %s. %s\n", arr[0], arr[1], arr[2], arr[3]);

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    77
    Quote Originally Posted by tabstop View Post
    You can put more than one thing in a format string, you know.
    Code:
    printf("%s. %s. %s. %s\n", arr[0], arr[1], arr[2], arr[3]);
    yes, it's good, but it I'll need with write a value to struct object?
    Code:
    struct data *p;
    p->str;

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by quantt View Post
    yes, it's good, but it I'll need with write a value to struct object?
    Code:
    struct data *p;
    p->str;
    What does your struct look like?

    Generally, you can use sprintf(p->str, ... ) to write to a string - but you may need more if p->str isn't an array.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    77
    What does your struct look like?

    Generally, you can use sprintf(p->str, ... ) to write to a string - but you may need more if p->str isn't an array.
    It's a good question, please look on my second post in that topic. I did need with quite good switch's or if's construction.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by quantt View Post
    It's a good question, please look on my second post in that topic. I did need with quite good switch's or if's construction.
    Your second post does not contain a definition of your struct.

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    77
    Quote Originally Posted by tabstop View Post
    Your second post does not contain a definition of your struct.
    Code:
    struct data *p;
    for (num=0;num<4; num++) {
    str += sprtinf(arr[num], sizeof(arr[num]));
    str += sprtinf(".");
    p->str;
    }
    I did defined a struct and and call from 0 to 3 char number, but I did need put that with separator similar of this...
    1.2.3.4
    Were numbers from 1 to 4 it's just char values from 0 to 3 char number.
    __
    thank in advance.

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    77
    If I doing which's I sampled, than I've got point on end of line.
    1.2.3.4.
    but me need that
    1.2.3.4
    Were numbers from 1 to 4 it's just char values, from 0 to 3 char numbers. A point it's the separator.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by quantt View Post
    Code:
    struct data *p;
    for (num=0;num<4; num++) {
    str += sprtinf(arr[num], sizeof(arr[num]));
    str += sprtinf(".");
    p->str;
    }
    I did defined a struct and and call from 0 to 3 char number, but I did need put that with separator similar of this...
    1.2.3.4
    Were numbers from 1 to 4 it's just char values from 0 to 3 char number.
    __
    thank in advance.
    So, well. That's certainly not how sprintf works, even if you spelled it correctly, and we certainly can't use += with C strings; nor can we be sure whether your str variable is a char [] (good) or just a char * (bad, or at least requiring more work). Just use one sprintf call with the format the way you want it (that's what the formatted output functions are for, after all). If you feel the need to use a loop for whatever reason, then you can just replace the period with a new-line after the loop.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    #include <stdio.h>
    
    int main()
    {
        char buffer[100];
        char *str = buffer;
    
        str += sprintf(str, "Hello, ");
        str += sprintf(str, "Wor");
        str += sprintf(str, "ld!\n");
    
        printf(buffer);  // Yes, I know, bad idea in case buffer contains %... 
        return 0;
    }
    This works fine - but as tabstop points out, there are problems with the usage of += stuff.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Jan 2009
    Posts
    77
    This works fine - but as tabstop points out, there are problems with the usage of += stuff.
    No, I understand it stuff.
    I ack which way doing it most good...
    Code:
    for (num=0;num<4; num++) {
    if(num=3){
    str += sprtinf(arr[num], ".");
    }else{
    str += sprtinf(arr[num]);
    }
    }
    May be you suggest make it too bonny?

  14. #14
    Registered User
    Join Date
    Jan 2009
    Posts
    77
    Why I can take there?
    Code:
    prg.c:(.text+0x133): undefined reference to `sprtinf'
    prg.c:(.text+0x148): undefined reference to `sprtinf'
    It's defined, but crashed.
    Code:
    unsigned char str;
    struct data *p;
    for (num=0;num<4; num++) {
    str += sprtinf(arr[num], sizeof(arr[num]));
    if(num < 3) str += sprtinf(".");
    }
    p->str;// I think what it shall be here, because str will be make after for issues.

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by quantt View Post
    Why I can take there?
    Code:
    prg.c:(.text+0x133): undefined reference to `sprtinf'
    prg.c:(.text+0x148): undefined reference to `sprtinf'
    It's defined, but crashed.
    Code:
    unsigned char str;
    struct data *p;
    for (num=0;num<4; num++) {
    str += sprtinf(arr[num], sizeof(arr[num]));
    if(num < 3) str += sprtinf(".");
    }
    p->str;// I think what it shall be here, because str will be make after for issues.
    Again, it's not called "sprtinf". Look at how sprintf works -- sprintf(arr[num], sizeof(arr[num])) is most assuredly not how it works even if you spelled it correctly -- sprintf works just like printf except the first argument is the string to print to. You also seem to think "p->str" means something; it does not.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  4. convert long to pointer to char array
    By gazmack in forum C++ Programming
    Replies: 5
    Last Post: 09-26-2003, 11:33 AM
  5. Searching a linked list for char
    By spentdome in forum C Programming
    Replies: 3
    Last Post: 05-22-2002, 11:11 AM