2

So I am writing a program that stores a user defined number of arrays, into a user defined sized array, currently I am trying to print out each string entered but do not know how to store away the previous strings to somewhere else. For example if the user wants 4 strings, I can only print out the last one. My first thought is to create another array for strings that grows as i put the user entered strings into it, but am lost on how to achieve this and seperate the strings in the bigger string. How do I repeat the printf statement at the end so that it prints out all of the strings entered and not just the last one? How do I create the space for the strings as they are given, and not overwrite them? How do I send the string somewhere else in memory before having the next string entered, and how would I access it? here is the code so far (work in progress)

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


int main(int argc,char *argv[]){

    int i, length, amt;
    char *ptr, *thetotalstring;
    i=0;

    printf("enter string amount: ");
    scanf("%d",&amt);

    for(;i<amt;i++){
        printf("Enter the length of the string: ");
        scanf("%d", &length);  //length is defined
        ptr = malloc(length/2*sizeof(char));  //allocate the space
        printf("Please enter string %d: ", (i+1));  
        scanf("%s", ptr);   //get the string
        thetotalstring = realloc(ptr, amt*length*sizeof(char));
    }  

    //allocate more 
    //space in a bigger string to hold other strings?? 
    for(i=0;i<amt;i++){
        printf("%s", thetotalstring);
    }
    return 0;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
steakfry
  • 21
  • 4

2 Answers2

0

You can use pointers ?

int *arrop[3]; 

example

int *arrop[3];
    int a = 10, b = 20, c = 50, i;

    arrop[0] = &a;
    arrop[1] = &b;
    arrop[2] = &c;

    for(i = 0; i < 3; i++)
    {
        printf("Current = %d\t Value = %d\n", arrop[i], *arrop[i]);
    }
Ashfaque Ali Solangi
  • 1,883
  • 3
  • 22
  • 34
0

The easiest thing to do would be to store an array of strings rather than trying to put them all into one big string buffer. First you'd allocate the array:

char **str_array = malloc(amt * sizeof(char*));

Then you'd allocate and populate each element of the array inside the loop:

str_array[i] = malloc(length * sizeof(char));
fgets(str_array[i], length, stdin); //safer than scanf

And finally you'd print each element of the array in the last loop:

printf("%s", str_array[i]);
manveti
  • 1,691
  • 2
  • 13
  • 16
  • Welcome to Stack Overflow! [don't cast malloc](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Barmar Dec 06 '18 at 00:43
  • @Barmar Too much time with C++ compilers... Removed the casts. – manveti Dec 06 '18 at 01:46
  • In other words, you want a separate array for each string, and an array of pointers to all those arrays. – user253751 Dec 06 '18 at 04:33
  • @Barmar so that part's unnecessary? I thought you had to allocate space everytime? How would you do it by NOT casting? – steakfry Dec 11 '18 at 23:17
  • @steakfry The allocation is necessary, but explicitly casting to the desired pointer type is not. I've spent a lot of time in C++, where the explicit conversion from void* to the desired pointer type is required, so my original answer had stuff like `char **str_array = (char**)malloc(amt * sizeof(char*));` – manveti Dec 12 '18 at 00:20