I am learning the basics of memory allocation in C(C++).
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
void main(){
char *str;
char *input;
int *ilist;
int i, size1, size2;
printf("Number of letters in word: ");
scanf("%d", &size1);
printf("Number of integers: ");
scanf("%d", &size2);
str = (char *)malloc(size1*sizeof(char) + 1);
ilist = (int *)malloc(size2*sizeof(int));
if (str == NULL || ilist == NULL){
printf("Lack of memory");
}
printf("Word: ");
int k = size1;
// the following line is done to prevent memory bugs when the amount of
letters in greater than size1.
scanf("%ks", &str); //I guess something is wrong with this line
/* user inputs a string */
for (i = 0; i < size2; i++) {
printf("Number %d of %d: ", i + 1, size2);
//this scanf is skipped during the execution of the program
scanf("%d", ilist + i);
}
free(str);
free(ilist);
system("pause");
}
The program asks user to write the amount of letters in the word and the amount of digits in the number. Then user writes the word. Then he writes integer one-by-one depending on what number was typed before. The problem I have is when the user writes the whole word, next scanf is skipped. Thank you. P.S. can there be other kind of memory bugs in this code?