#include <stdio.h>
int main() {
int buf[1024];
if (scanf("%1023[^\n]", buf) == 1) {
int *myarray = strdup(buf); // strdup should use malloc to dynamically allocate basing on the length of string
if (myarray) {
printf("Value: %s\nLength of string: %d\nDynamically allocated space: %d\n", myarray, strlen(myarray), ( (strlen(myarray) + 1)*sizeof(int) )); // the last one should be the value of the new dyn allocated "myarray" array
free(myarray);
}
}
return 0;
}
Is this the best way to avoid memorization problems with scanf?
Can I use realloc in some way instead of using strdup and buf?
Does the part (strlen(myarray) + 1)*sizeof(int) correctly prints the size in bytes of the dynamically allocated myarray with strdup?