putting values into array-newbie doubt

Discussion in 'C Programming' started by oswald.harry@gmail.com, Jan 29, 2008.

  1. Guest

    hi
    i am trying to read from cmd line some strings and put them into an
    array of char* ,i want to use this array lateron so i wrote

    int main(int argc,char* argv[]) {
    int i=0;
    int totalnames=argc-1;
    char* names=malloc(sizeof(char*)* totalnames);

    printf("totalnames:%d\n",totalnames);
    for (i=0;i<totalnames;i++){
    printf("name:%d=%s\n",i+1,argv[i+1]);
    names=argv[i+1];
    printf("names[%d]is %s\n",i,names);

    }

    }

    i get stackdump at the second printf in the for loop..can someone help
    out?
    thanx
    oharry
     
    , Jan 29, 2008
    #1
    1. Advertising

  2. Guest

    On Jan 29, 7:00 pm, wrote:
    > hi
    > i am trying to read from cmd line some strings and put them into an
    > array of char* ,i want to use this array lateron so i wrote
    >
    > int main(int argc,char* argv[]) {
    > int i=0;
    > int totalnames=argc-1;
    > char* names=malloc(sizeof(char*)* totalnames);

    Your calculations are wrong, you either want a sizeof(char) or a char
    ** names.
    In the first case you don't need it because sizeof(char) == 1.
    Also, you don't check the return value of malloc.
     
    , Jan 29, 2008
    #2
    1. Advertising

  3. santosh Guest

    wrote:

    > hi
    > i am trying to read from cmd line some strings and put them into an
    > array of char* ,i want to use this array lateron so i wrote
    >
    > int main(int argc,char* argv[]) {
    > int i=0;
    > int totalnames=argc-1;


    The argument 'argc' can be zero or more. If it is one it is almost
    always the program's name that is in argv[argc-1].

    > char* names=malloc(sizeof(char*)* totalnames);


    This is probably wrong. What you might want is:

    char **names = malloc( totalnames * sizeof *names );
    if (!names) { handle_error(); }
    else {
    for (i = 0; i < totalnames; i++) {
    names = malloc( strlen(argv[i+1] + 1) * sizeof **names );
    if (!names) { handle_error(); }
    }
    }
    printf("totalnames: %d\n", totalnames);
    for (i = 0; i < totalnames; i++) strcpy(names, argv[i+1]);
    for (i = 0; i < totalnames; i++) {
    printf("name: %d = %s\n", i+1, names);
    }

    /* free memory before exit */

    > printf("totalnames:%d\n",totalnames);
    > for (i=0;i<totalnames;i++){
    > printf("name:%d=%s\n",i+1,argv[i+1]);
    > names=argv[i+1];
    > printf("names[%d]is %s\n",i,names);
    >
    > }
    >
    > }
    >
    > i get stackdump at the second printf in the for loop..can someone help
    > out?
    > thanx
    > oharry
     
    santosh, Jan 29, 2008
    #3
  4. In article <fnns7t$rni$>,
    santosh <> wrote:
    >The argument 'argc' can be zero or more. If it is one it is almost
    >always the program's name that is in argv[argc-1].


    Except when it isn't. And "program's name" might mean
    the filename (no path) and extension or it might mean the base filename
    (no path and no extension), or it might mean the entire path
    to the executable as entered by the user or program that created
    the call, or it might mean the fully resolved path to the executable
    (possibly after following all symbolic links). And it is not
    uncommon for daemons to be launched with the argv[0] set to
    some human readable phrase instead of to a program name; for example,
    netscape 4 spun off a process whose argv[0] was '(dns helper)'
    --
    So you found your solution
    What will be your last contribution?
    -- Supertramp (Fool's Overture)
     
    Walter Roberson, Jan 29, 2008
    #4
  5. On 29 jan, 14:00, wrote:
    > int main(int argc,char* argv[]) {
    > int i=0;
    > int totalnames=argc-1;
    > char* names=malloc(sizeof(char*)* totalnames);


    If you say you want to put in an array of char* thats different than
    char*
    You see, char* foo[] is an array of char*, but char* names=... is not.

    > printf("totalnames:%d\n",totalnames);
    > for (i=0;i<totalnames;i++){
    > printf("name:%d=%s\n",i+1,argv[i+1]);
    > names=argv[i+1];


    Why don't start i with 1? then you don't need to argv[i+1]...

    > printf("names[%d]is %s\n",i,names);
    >
    > }
    >
    > }
    >
    > i get stackdump at the second printf in the for loop..can someone help
    > out?
    > thanx
    > oharry
     
    Leonardo Korndorfer, Jan 29, 2008
    #5
  6. Guest

    thanx santhosh..now i realise my mistake..
    oharry
     
    , Jan 30, 2008
    #6
  7. h03Ein Guest

    wrote:
    > hi
    > i am trying to read from cmd line some strings and put them into an
    > array of char* ,i want to use this array lateron so i wrote
    >
    > int main(int argc,char* argv[]) {
    > int i=0;
    > int totalnames=argc-1;
    > char* names=malloc(sizeof(char*)* totalnames);
    >
    > printf("totalnames:%d\n",totalnames);
    > for (i=0;i<totalnames;i++){
    > printf("name:%d=%s\n",i+1,argv[i+1]);
    > names=argv[i+1];
    > printf("names[%d]is %s\n",i,names);
    >
    > }
    >
    > }
    >
    > i get stackdump at the second printf in the for loop..can someone help
    > out?
    > thanx
    > oharry


    the line

    char* names=malloc(sizeof(char*)* totalnames);

    indicates that names is pointer to characters(in other words array of
    chars) not array of array of chars so your definition should be like
    this :

    char **names=(char **)malloc(sizeof(char*)* totalnames);

    and a soultion for this problem may look like:

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    int main(int argc,char* argv[]) {
    int i=0;
    int totalnames=argc-1;
    char **names=(char **)malloc(sizeof(char*)* totalnames);


    printf("totalnames:%d\n",totalnames);
    for (i=0;i<totalnames;i++){
    printf("name:%d=%s\n",i+1,argv[i+1]);
    names=argv[i+1];
    printf("names[%d]is %s\n",i,names);


    }

    }
     
    h03Ein, Jan 30, 2008
    #7
  8. Ian Collins Guest

    h03Ein wrote:
    >
    > the line
    >
    > char* names=malloc(sizeof(char*)* totalnames);
    >
    > indicates that names is pointer to characters(in other words array of
    > chars) not array of array of chars so your definition should be like
    > this :
    >
    > char **names=(char **)malloc(sizeof(char*)* totalnames);
    >

    Forget the cast, the idiomatic form is

    char **names = malloc( totalnames * sizeof *names );

    --
    Ian Collins.
     
    Ian Collins, Jan 30, 2008
    #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. Saurabh
    Replies:
    6
    Views:
    4,542
    Chris Smith
    May 30, 2004
  2. Kai Jaensch
    Replies:
    3
    Views:
    440
    Kajoka
    Jan 15, 2004
  3. Kai Jaensch

    splitting a string and putting it into an array?

    Kai Jaensch, Jan 14, 2004, in forum: C Programming
    Replies:
    1
    Views:
    342
    Sean Kenwrick
    Jan 14, 2004
  4. Kajoka
    Replies:
    0
    Views:
    302
    Kajoka
    Jan 14, 2004
  5. jackster the jackle

    Putting string values into array

    jackster the jackle, Nov 21, 2008, in forum: Ruby
    Replies:
    2
    Views:
    95
    Mark Thomas
    Nov 21, 2008
Loading...

Share This Page