|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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[i]=argv[i+1]; printf("names[%d]is %s\n",i,names[i]); } } i get stackdump at the second printf in the for loop..can someone out? thanx oharry |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On Jan 29, 7:00 pm, oswald.ha...@gmail.com 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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
oswald.harry@gmail.com 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[i] = malloc( strlen(argv[i+1] + 1) * sizeof **names ); if (!names[i]) { handle_error(); } } } printf("totalnames: %d\n", totalnames); for (i = 0; i < totalnames; i++) strcpy(names[i], argv[i+1]); for (i = 0; i < totalnames; i++) { printf("name: %d = %s\n", i+1, names[i]); } /* 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[i]=argv[i+1]; > printf("names[%d]is %s\n",i,names[i]); > > } > > } > > i get stackdump at the second printf in the for loop..can someone > out? > thanx > oharry |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
In article <fnns7t$rni$1@registered.motzarella.org>,
santosh <santosh.k83@gmail.com> 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 er)' -- So you found your solution What will be your last contribution? -- Supertramp (Fool's Overture) |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On 29 jan, 14:00, oswald.ha...@gmail.com 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[i]=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]); > > } > > } > > i get stackdump at the second printf in the for loop..can someone > out? > thanx > oharry |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
thanx santhosh..now i realise my mistake.. oharry |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
oswald.ha...@gmail.com 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[i]=argv[i+1]; > printf("names[%d]is %s\n",i,names[i]); > > } > > } > > i get stackdump at the second printf in the for loop..can someone > 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[i]=argv[i+1]; printf("names[%d]is %s\n",i,names[i]); } } |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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. |
|
![]() |
| Outils de la discussion | |
|
|