putting values into array-newbie doubt

O

oswald.harry

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
 
V

vippstar

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.
 
S

santosh

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
 
W

Walter Roberson

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)'
 
L

Leonardo Korndorfer

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
 
H

h03Ein

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


}

}
 
I

Ian Collins

h03Ein said:
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 );
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top