sprintf and array of pointers - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

sprintf and array of pointers

rakiorakio Posts: 18Member
hi ,
Ia m not getting the output can anyone tell me. whts wrong in this program, I want to load the string "s" into array of pointer sting "*n[20]".


<{ int i=1,j=0;>






<{>

< { *s4=*p;>



<{>

< case 2:
s4=s3;
break;>
< default: break;
}>



< for(j=0;j<4;j++)>
< {
printf("%s",*n[j]);
}>
< }>

«1

Comments

  • MT2002MT2002 Posts: 1,444Member
    Please use [leftbr]code[rightbr]*code here*[leftbr]/code[rightbr] when posting code o help perserve formatting:
    [code]
    Example
    [/code]
    It will make your code much more readable.
    [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS Operating System[rightbr][leftbr][link=http://www.mt2002.sitesled.com]Website[rightbr][/link][/size]
  • BitByBit_ThorBitByBit_Thor Posts: 2,444Member
    Do you want to break the string up into pieces using a space (' ') as a delimiter?

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • rakiorakio Posts: 18Member
    : Do you want to break the string up into pieces using a space (' ')
    : as a delimiter?
    :
    : Best Regards,
    : Richard
    :
    : The way I see it... Well, it's all pretty blurry


    yes i want to break the string and copy it into array ponter sting.
    thank you
    rakio.
  • bilderbikkelbilderbikkel Posts: 754Member
    main() has return type int.

    [code]
    int main() { }
    [/code]
    bilderbikkel
  • BitByBit_ThorBitByBit_Thor Posts: 2,444Member
    :
    : yes i want to break the string and copy it into array ponter sting.
    : thank you
    : rakio.
    :

    I just realised as I was writing this post that I could write it for you. But that ofcourse wouldn't be fun for you.

    So here's the logic:

    Go through the base string one character at a time
    Check if it is a space.
    If so, select the bit of string between the found spaces (this one and the previous space). Then copy this into an array of pointers (don't forget to reserve memory first!), and also don't forgot to null-terminate the strings (don't forget to reserve enough memory for this too!). Remember the location of the current space for the next space you find (so you can select the string between the two spaces).
    Continue till at end of string.

    There are some thing you have to think of, like what variables to use and what their initial values should be. Also, there is a problem at the end of the logic (it's missing something) that you have to fix.

    Some basics on memory reserving:
    [code]
    #include

    char* s = (char*) malloc(25); /* reserves 25 bytes of memory */
    strcpy(s, "Hello this is a string"); /* it's already 0-terminated */

    /* When you're done with it, use free() to free the memory */
    /* This is very important, so don't forget it */
    free(s);
    [/code]

    If you want an array of strings, easiest is to just declare a static size for the array:
    [code]
    #define ARRAY_SIZE 10

    char* array[ARRAY_SIZE];
    /* Now too you have to allocate the room before you use it */
    array[0] = (char*) malloc( 20 );
    array[1] = (char*) malloc( 2 );
    array[2] = (char*) malloc( 45 );
    ...
    /* array[0] can now contain 19 characters and a terminating 0 */
    /* array[1] can only hold one char and a 0-termination */
    /* array[2] 44 characters */
    /* etc */

    /* Just one more thing: to acces the 5th item of the third piece: */
    fifth_item_of_the_third_piece = array[2][4];
    /* First item is 0, second is 1, etc */
    [/code]

    Enjoy :)



    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • rakiorakio Posts: 18Member
    Thank you richrd i got the output thanx a lot i learnt something.
    rakio
  • rakiorakio Posts: 18Member
    Hi Richard dont think i am asking too much ,can i get a project in c which is not related to embedded.
    I am looking for nice job.
    rakio
  • BitByBit_ThorBitByBit_Thor Posts: 2,444Member
    : Hi Richard dont think i am asking too much ,can i get a project in c
    : which is not related to embedded.
    : I am looking for nice job.
    : rakio
    :

    You can never ask too much ;)

    But I am not entirely sure I understand your question.
    You're wondering whether there are business C projects that are not about Embedded software?
    I guess most projects these days are. But it's quite the same as programming in any OS. Basically, you are given a Library of functions to interract with your environment (for instance the crt headers) and you use that and basic language elements to create a program.

    Ofcourse, if you really want to stay with OS programs (like applications for *nix and Windows) then most likely you'll be programming games or software interfaces for drivers and hardware.

    I hope that was your question and that I answered it to satisfaction.
    On second reading: are you looking for idea's for new projects in C?

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • rakiorakio Posts: 18Member
    Thankk you for the reply ,you got it right. I dont want to go into embedded but i want to do C rpojects . I am in my learning curve of C.
    I dont know can you be my mentor in guiding me. I am working as an trainee in a small company ,now the trainiing is over for c and with in few days C++ will be started and then I will be given tarining for Symbian OS(mobile OS).I want to be number one no matter what happens, if you think you can help me by making me perfect in C and C++ it will be greatful.
    Hey its a requet no compulsion. I am asking you to help me because there is no one around me to rely upon.
    its ok if you are busy Richard.
    rakio.
  • BitByBit_ThorBitByBit_Thor Posts: 2,444Member
    : Thankk you for the reply ,you got it right. I dont want to go into
    : embedded but i want to do C rpojects . I am in my learning curve of
    : C.
    : I dont know can you be my mentor in guiding me. I am working as an
    : trainee in a small company ,now the trainiing is over for c and with
    : in few days C++ will be started and then I will be given tarining
    : for Symbian OS(mobile OS).I want to be number one no matter what
    : happens, if you think you can help me by making me perfect in C and
    : C++ it will be greatful.
    : Hey its a requet no compulsion. I am asking you to help me because
    : there is no one around me to rely upon.
    : its ok if you are busy Richard.
    : rakio.
    :

    Well the offer does sound tempting :)

    But you should know I am not at all a C nor C++ guru. I'm a die-hard VB6'er, who knows a bit of C, C++ and ASM. Most of the things I know, but often I look through documentations etc to find the answers to problems.
    The point is I haven't programmed all that much in C and C++, so I forget a lot of things (and thus need to look them up).
    But, you can always come to me or to the boards with questions.

    If you still would like my help, we could also mail eachother (so we don't spam the boards).
    I am glad to see you like C and it's always good to want to be the best! :D


    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
«1
Sign In or Register to comment.