-1

First of all, neither part of my code achieves what I desire. Secondly, I get different output when I use a function with the same piece of code. The goal is to enter multiple separate strings, save each as a following element of an array, and then print each element. Yet for the code included in main() I receive six times the last string entered:

This one runs in main:

Enter 6 strings to be printed:
1
2
3
4
5
6
Printing input:
6
6
6
6
6
6

and when the exact same piece of instructions is summoned in a function I'm getting empty strings (6 times the "new tab" in a console):

This one is exactly the same & runs in external function:

Enter 6 strings to be printed:
1
2
3
4
5
6
Printing input:

Now I do know that variables in a function exist only in the scope of this function, but still I do not understand this behavior. What should I do to achieve this effect using arrays, both to work in a function and in main?

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

void printStrings (void) {
    printf("Enter 6 strings to be printed:\n");
    
    char *tmp_ans[6];
    char *tmp;
    
    for (int j = 0; j < 6; j++) {
        tmp_ans[j] = gets(tmp);
    }
    
    printf("Printing input:\n");
    
    for (int i = 0; i < 6; i++) {
        printf("%s\n", tmp_ans[i]);
    }
}

int main() {
    printf("This one runs in main:\n\n");
    printf("Enter 6 strings to be printed:\n");
    
    char *tmp_ans[6];
    char *tmp;
    
    for (int j = 0; j < 6; j++) {
        tmp_ans[j] = gets(tmp);
    }
    
    printf("Printing input:\n");
    
    for (int i = 0; i < 6; i++) {
        printf("%s\n", tmp_ans[i]);
    }
 
    printf("This one is exactly the same & runs in external function:\n\n");
    
    printStrings();
    
return (EXIT_SUCCESS);
}

I know that gets() is bad/evil/etc, but for my own purpose it is just enough and I use it because it's simple.

letsintegreat
  • 3,328
  • 4
  • 18
  • 39
  • 1
    This statement tmp_ans[j] = gets(tmp); invokes undefined behavior because the pointer tmp was not initialized. – Vlad from Moscow Jan 25 '21 at 10:58
  • 1
    Your code exhibits **undefined behaviour** in both cases - so, whether or not you get the same/correct/random output is unpredictable. You are attempting to read data into the `tmp_ans[i]` buffers, but these are uninitialized pointers. – Adrian Mole Jan 25 '21 at 10:58
  • 1
    In maybe even more clear way: gets() expects a pointer to some memory which is ready for storing a astring. But your tmp variable points nowhere (or probably to some random location), so gets() call overwrites something somewhere in the memory and corrupts the program. You have to allocate the memory for each string. The way you use char pointers suggests that you maybe know some higher level programming language like Python or Javascript, where the language does this for you. But in C you are responsible for what is where in the memory. No garbage collector. No nothing, just you. – petrch Jan 25 '21 at 11:06
  • In your own words, what do you think a `char *` is? Are you perhaps expecting it to store the actual text when you make a `gets` call? If so, how much, and why? If not, where do you expect it will be stored? – Karl Knechtel Jan 25 '21 at 11:09
  • Please read for example http://c-faq.com/aryptr/ . – Karl Knechtel Jan 25 '21 at 11:10
  • @KarlKnechtel What I thought until now was that I can create a pointer to memory without allocating specific size of that memory, more precisely - I thought that there would be amount of memory allocated automatically, just the size of the input. – Janek Zasławski Jan 25 '21 at 11:22
  • Pointers are not pointing at _any_ memory until you tell them to do so. Check the duplicate post I linked for examples. – Lundin Jan 25 '21 at 11:57

2 Answers2

0

Apart from the horror of using gets, you land straight into undefined behaviour as the str variable is not initialized and thus points to a random location.

consider this:

char tmp[256];
for (int j = 0; j < 6; j++) {
    fgets(tmp, sizeof(tmp), stdin);
    tmp_ans[j] = strdup(tmp);
}

Note I left out error checking. You should add that.

koder
  • 2,038
  • 7
  • 10
0

your code is undefined behaviour, you don't allocate memory where to store what you read:

char *tmp; // <= no memory allocated for storage

for (int j = 0; j < 6; j++) {
    tmp_ans[j] = gets(tmp); // <= gets call is undefine behaviour
}

you could maybe create an array on the stack char tmp[1024] but anyway, gets is still not a good idea.

OznOg
  • 4,440
  • 2
  • 26
  • 35