PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > putting values into array-newbie doubt
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
putting values into array-newbie doubt

Réponse
 
LinkBack Outils de la discussion
Vieux 29/01/2008, 17h00   #1
oswald.harry@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut putting values into array-newbie doubt

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
  Réponse avec citation
Vieux 29/01/2008, 17h08   #2
vippstar@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: putting values into array-newbie doubt

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.
  Réponse avec citation
Vieux 29/01/2008, 18h45   #3
santosh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: putting values into array-newbie doubt

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


  Réponse avec citation
Vieux 29/01/2008, 18h51   #4
Walter Roberson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: putting values into array-newbie doubt

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)
  Réponse avec citation
Vieux 29/01/2008, 19h27   #5
Leonardo Korndorfer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: putting values into array-newbie doubt

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


  Réponse avec citation
Vieux 30/01/2008, 04h40   #6
oswald.harry@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: putting values into array-newbie doubt


thanx santhosh..now i realise my mistake..
oharry
  Réponse avec citation
Vieux 30/01/2008, 07h59   #7
h03Ein
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: putting values into array-newbie doubt



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


}

}
  Réponse avec citation
Vieux 30/01/2008, 08h11   #8
Ian Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: putting values into array-newbie doubt

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.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 19h23.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13643 seconds with 16 queries