0

So my problem is here:

char *input;
char *takenpositions[18] ={"A0","A0","A0" /* etc. */};

int k;

for(k = 0; k < 18; k++) {

  scanf("%s",&input);

  /* ...
     The program is doing other things with input here, then i want to put it
     into the array in place of the A0s. I tried strncpy, and other things but
     maybe i couldn't use it correctly.
     ...
  */

  takenpositions[k] = input;
}

I couldn't find the answer maybe because it's too easy or I'm just lame.

Ruslan Osmanov
  • 20,486
  • 7
  • 46
  • 60
  • You have not allocated memory for "input". So you can not call "scanf("%s",&input);". If you want to read a string and store it's pointer you have to allocate memory. – MayurK Oct 22 '16 at 15:32
  • @MayurK - and even if memory had been allocated,you wouldn't type `&input` there, you'd just give the pointer to the memory (which input holds) - passing `&input` has the effect of passing the address of the variable that holds the address of the available mem. The correct usage would be `scanf("%s",input);` – enhzflep Oct 22 '16 at 15:36
  • @enhzflep Yes. I have made that change in my answer. – MayurK Oct 22 '16 at 15:38
  • Please turn up compiler warning level (*-Wall -Wextra* for *gcc* and *clang*), so it warns about bad format strings. Also check return value of *scanf* to avoid nasty surprises with invalid input. – hyde Oct 22 '16 at 15:46
  • @hyde I will do, thanks! – Balázs Sipos Oct 22 '16 at 15:51

1 Answers1

1

As I mentioned in the comment, you need to allocate memory for "input". Probably this is what you are trying to do.

#define MAX_STR_LEN 256
char *input;
char *takenpositions[18] ={0}; //Initialize all pointers to NULL (0).

int k;

for(k = 0; k < 18; k++) {

    input = malloc(sizeof(char)*(MAX_STR_LEN+1)); //Allocate memory

    char scanfString[32] = ""; //32 characters should be sufficient for scanf string.

    //To limit number of character inputs use string "%<limit>s" in scanf()
    sprintf(scanfString, "%%%us", MAX_STR_LEN);


    scanf(scanfString, input);

  /*
     Your code.
  */

  takenpositions[k] = input; //Save pointer.
}
MayurK
  • 1,925
  • 14
  • 27
  • I was trying to do this all afternoon... :D – Balázs Sipos Oct 22 '16 at 15:48
  • Of course with this code, if input is longer than 256 chars, you get buffer overflow. Writing code which may do this is *bad*, even when just learning C, because bad habits carry over, and double bad when written to Stack Overflow as advice for anyone searching help for similar problem. – hyde Oct 22 '16 at 15:49
  • @hyde Yes. that is the limitation in my answer. I will think about making this answer generic. I will update it soon. Thank you :) – MayurK Oct 22 '16 at 15:53
  • @hyde I have modified my answer to allow only limited number of characters in scanf(). I referred http://stackoverflow.com/questions/1621394/how-to-prevent-scanf-causing-a-buffer-overflow-in-c and http://stackoverflow.com/questions/3042980/how-to-print-s-in-c links for this change. – MayurK Oct 22 '16 at 16:58