Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Example How many names: 3
Name 1: Jack
Name 2: Tom
Name 3: John
Those 3 names are: Jack Tom John.

What I have tried:

#include<stdio.h>
#include<string.h>
int i,n;
char s[50],array[50];
main(){
printf("How many names : ");
scanf("%d",&n);
for (i=0;i<n;i++){
printf("Name %d : ",i+1);
gets(s);

after that i tried to assign array[i]=s , it doesn't work
and i tried alot more but it keep displaying null.
Posted
Updated 29-Jun-22 8:03am
Comments
Patrice T 24-Dec-17 5:22am    
What do you mean by:
'C programming: how do I store names in one dimensional array ?'
What do you want to do with the 3 names and the 1d variable?
Do you want to build a large string or simulate a 2d array ?

You read a string into the array s but you never do anything with it. If you want to save the names then you need to copy each one to its own array. the simplest way to do it is by creating a 2 dimensional array, something like:
C++
char names[4][16]; // 4 arrays each of 16 characters
// in the loop
for (i=0;i<n;i++)
{
    printf("Name %d : ",i+1);
    // gets(names[i]);
    fgets (names[i], 16, stdin);
}

However, since you do not know how many arrays there are in the first place you will need to do some dynamic allocation, and use character pointers. Something like:
C++
char** names; // a pointer to an unknown number of arrays
names = (char**)malloc(n * sizeof(char*)); // allocate the array pointers
// in the loop
for (i=0;i<n;i++)
{
    printf("Name %d : ",i+1);
    names[i] = (char*)malloc(16); // allocate an array for the next name
    //gets(names[i]);
    fgets (names[i], 16, stdin);
}
 
Share this answer
 
v2
The problem is that "names" are strings: each one is an array of char
char name1[] = "Jack";
printf("%s\n", name1);
char name2[] = "Tom";
printf("%s\n", name2);
So you can't easily keep them in a one dimensional array like yours.
What you need is a one dimensional array of pointer to char values, each of which is a separate name:
char* names[] = {"Jack", "Tom", "John"};
for (int i = 0; i < 3; i++)
   {
    printf("%s\n", names[i]);
   }

To read them in from the user is more complicated: you need to use malloc to allocate space for the data the user is going to type before each call to scanf, and store the pointer it returns in your names array.
 
Share this answer
 
C
int n = 0;
printf("How many names : ");
scanf("%d", &n);

Name* namelst = (Name*)malloc(n * NAMESIZE));

clearline();
    
for (int i = 0; i < n; i++) {
    printf("Name %d : ", i + 1);
    fgets(namelst[i], NAMESIZE , stdin);
}

with
C
#define NAMESIZE 25
typedef  char Name[NAMESIZE];
void clearline() 
{ for (int ch = 0; (ch != EOF) && (ch != '\n'); ch = fgetc(stdin)); }
 
Share this answer
 
Comments
Patrice T 29-Jun-22 14:13pm    
2017 !
merano99 29-Jun-22 17:17pm    
No, it could have been coded like that in the early 80s. I gave Richard's dynamic solution a 5, but I also wanted to offer a simple alternative here with only one malloc.

The Task was to store names in one dimensional array. See Title.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900