I want to hold user input strings in an array and be able to print it. Is there a way for me to hold string 1 in arr[0], string 2 in arr[1] and so on and so forth?

heres what Im thinking

int p=1, r=3, l=0;
    char arr[255];
    
    while(p <= r)
    {
            printf("Please enter string#%d: ",p);
            scanf("%s", &arr[l]);
            l++;
            p++;  
    }

Recommended Answers

All 5 Replies

If you know ahead of time the maximum number of strings you will input and the maximum length of each string you can make a 2D array of chars as follows

char arr[max strings][max length];

then you can use

scanf("%s", &arr[l])

to input each string and the same for printing them out.

The way you have it will only store 255 characters, not 255 strings. Make your array 2D. For example, char arr[255][10]; would allow you to store 255 strings of 9 characters each (plus a '\0'). You could enter strings into them by using fgets(arr[i],sizeof(arr[i]),stdin); where I goes from 0 to 254. Just remember if there is room the fgets will put the newline character into the array from when you pressed enter. You could use scanf also but make sure you limit your input with the specifier to the number of characters you designate.

EDIT: He's got it ^^^^^^^^^^^^^^

And by combining all the ideas you get a nice code looking like this:

#include <stdio.h>
#include <string.h>


int main()
{
  int maxlength,maxstring=5,i=0;

  printf("\nEnter the size for the characters:");
  scanf("%d",&maxlength);

  char str_array[maxstring][maxlength];

  while(i<maxstring)
 {
    printf("\nEnter string:");
    scanf("%s",&str_array[i]);
    i++;
 }


 i = 0;

  while(i<maxstring)
 {
    printf("\n%s",str_array[i]);
    i++;
 } 

 return 0;
}

>you get a nice code looking like this
Well, it could be worse.

>printf("\nEnter the size for the characters:");
There are three times when the output buffer is flushed:

  1. It becomes full.
  2. A newline character is printed.
  3. The code calls fflush on the output stream.

For obvious reasons it's silly to rely on #1, and you did neither #2 nor #4. So you've got a bug where the prompt may not show up before scanf blocks for input. The end result is a user sitting there not knowing what to do.

>scanf("%d",&maxlength);
Friends don't let friends call scanf without testing the return value. I'm not your friend, so I'll just warn you that it's walking on thin ice to use a formatted input function for user input (the least formatted input there is), and expect it to work without sanity checks.

>char str_array[maxstring][maxlength];
This is only guaranteed to work in C99.

>scanf("%s",&str_array);
The address-of operator actually does something. Since str_array is already a pointer to char, adding the address-of operator creates a type mismatch. Just because many compilers let you get away with it doesn't mean it's not a bug.

>printf("\n%s",str_array);
I notice this habit of putting the newline on the wrong side of the format string amongst beginners. Does "someoutputfromthelastprogramC:\> _" actually look good to you? Stream I/O is tricky enough to get right without adding your own idiosyncrasies to the mix. :icon_rolleyes:

Thanks for all the knowledge, it will help me in future.

In case you were wondering, yes, I do hate you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.