Solved

fgets,gets,scanf and array of char pointers

Posted on 1997-10-31
8
711 Views
Last Modified: 2012-08-14
I am trying to learn array of pointers:
Please show a SIMPLE Example to show me how to input data into
*names[4];
with scanf,gets,fgets.
Thank you, John
0
Comment
Question by:johnm112496
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1256040
#define len      20
#define slen      "20"
char *names[4];
for(i=0;i<4;i+=1){ names[i] = (char *)malloc(len+1); }
scanf("%" slen "s", names[0]);
gets(names[1]);
fgets(names[2],len,stdin);
printf("%s, %s, %s",names[0],names[1],names[2]);
0
 

Author Comment

by:johnm112496
ID: 1256041
FOR OZO ONLY
can the same be done with:

gets (names[i])??
John

Please send me your EMail @ msd@bendnet.com
0
 

Author Comment

by:johnm112496
ID: 1256042
Adjusted points to 200
0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 10

Accepted Solution

by:
rbr earned 200 total points
ID: 1256043
Sure the same can be done with gets(names[i]) also if i is in the range of 0 to 3 of your example.
 If you work with arraypointers  you must always be sure that you reserve
enough memory. This can be done with malloc or I prefer calloc since this clears
to memory you get from your OS.
 
0
 
LVL 2

Expert Comment

by:wpinto
ID: 1256044
Hey ozo,

Why don't you give answers rather than comments?

You always get there first, then wait for someone else to give your comments as their answers

Wilfred
0
 
LVL 84

Expert Comment

by:ozo
ID: 1256045
Since I can't tell what the questioner will consider an answer before
they comment back, I prefer not to lock the question until then.
Sometimes questions here seem to degenerate into a race to lock
rather than an attempt to find the best answer, and I don't want to
participate in such a competition.
Sometimes finding answers requires the cooperation of several commentators.
Others may have a different policy, but that's up to them.
The questioner always has the final say about what's an answer in any case.
0
 
LVL 2

Expert Comment

by:wpinto
ID: 1256046
Hey ozo,

I can't argue with that!!

Hats off to you

Cheers,
Wilfred

PS. Sorry John, for diverting from the topic
0
 

Author Comment

by:johnm112496
ID: 1256047
How do you allocate memory while the user inputs the strings with gets(string[i]);  this should allow the computer all the mem allocation for you right? John
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
voltage to force translation ? 8 99
Test against App 49 138
UPD maximums on Red Hat 6 120
Windows Server 2012  Backup - HyperV host 18 54
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question