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.

5
  • 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. Commented Oct 22, 2016 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); Commented Oct 22, 2016 at 15:36
  • @enhzflep Yes. I have made that change in my answer. Commented Oct 22, 2016 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. Commented Oct 22, 2016 at 15:46
  • @hyde I will do, thanks! Commented Oct 22, 2016 at 15:51

1 Answer 1

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.
}
Sign up to request clarification or add additional context in comments.

4 Comments

I was trying to do this all afternoon... :D
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 Yes. that is the limitation in my answer. I will think about making this answer generic. I will update it soon. Thank you :)
@hyde I have modified my answer to allow only limited number of characters in scanf(). I referred stackoverflow.com/questions/1621394/… and stackoverflow.com/questions/3042980/how-to-print-s-in-c links for this change.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.