Hello all,
I've been trying to work out how to dynamically input strings---i.e. to be able to input a string without first deciding what size it should be. Here's a test program:
Now, if I just use [tt]gcc iotest.c -o iotest[/tt], everything is fine, and it runs great. But stricter control of dialect involves problems...
e.g. [tt]gcc -ansi -pedantic -Wall iotest.c -o iotest[/tt] produces,
[tt]iotest.c: In function ‘main’:
iotest.c:13: warning: implicit declaration of function ‘asprintf’
iotest.c:23: warning: ISO C does not support the 'a' scanf flag[/tt]
While if I instead use [tt]gcc -std=gnu89 -Wall iotest.c -o iotest[/tt], I get,
[tt]iotest.c: In function ‘main’:
iotest.c:13: warning: implicit declaration of function ‘asprintf’[/tt]
The first I kind of expect since AFAIK the extras I'm using here are GNU extensions, but why does an error still come up when requesting the gnu89 standard? Using [tt]gcc -std=gnu99 -Wall iotest.c -o iotest[/tt] is even worse:
[tt]iotest.c: In function ‘main’:
iotest.c:13: warning: implicit declaration of function ‘asprintf’
iotest.c:23: warning: format ‘%a’ expects type ‘float *’, but argument 2 has type ‘char **’[/tt]
Anyway, my questions are... (1) what options do I need to not get an objection to what I'm doing here? And (2) is there a better way of achieving this goal, such that I can be compatible with the strict [tt]-ansi -pedantic[/tt] compatibility that I like to use?
Thanks in advance.
I've been trying to work out how to dynamically input strings---i.e. to be able to input a string without first deciding what size it should be. Here's a test program:
Code:
/* iotest.c */
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int i, stringsize;
char info1[127], info2[127];
char *info;
sprintf(info1,"This is");
sprintf(info2,"a very silly string.");
printf("%s %s\n",info1,info2); fflush(stdout);
stringsize = asprintf(&info,"%s %s",info1,info2);
printf("%s\n",info); fflush(stdout);
for(i=0;i<stringsize;++i)
printf("%c",info[i]);
printf("\nSize = %d\n\n",stringsize);
free(info);
printf("Please enter an arbitrary string: ");
scanf("%as",&info);
stringsize = asprintf(&info,"%s",info);
printf("%s\n",info); fflush(stdout);
for(i=0;i<stringsize;++i)
printf("%c",info[i]);
printf("\nSize = %d\n\n",stringsize);
free(info);
return 0;
}
Now, if I just use [tt]gcc iotest.c -o iotest[/tt], everything is fine, and it runs great. But stricter control of dialect involves problems...
e.g. [tt]gcc -ansi -pedantic -Wall iotest.c -o iotest[/tt] produces,
[tt]iotest.c: In function ‘main’:
iotest.c:13: warning: implicit declaration of function ‘asprintf’
iotest.c:23: warning: ISO C does not support the 'a' scanf flag[/tt]
While if I instead use [tt]gcc -std=gnu89 -Wall iotest.c -o iotest[/tt], I get,
[tt]iotest.c: In function ‘main’:
iotest.c:13: warning: implicit declaration of function ‘asprintf’[/tt]
The first I kind of expect since AFAIK the extras I'm using here are GNU extensions, but why does an error still come up when requesting the gnu89 standard? Using [tt]gcc -std=gnu99 -Wall iotest.c -o iotest[/tt] is even worse:
[tt]iotest.c: In function ‘main’:
iotest.c:13: warning: implicit declaration of function ‘asprintf’
iotest.c:23: warning: format ‘%a’ expects type ‘float *’, but argument 2 has type ‘char **’[/tt]
Anyway, my questions are... (1) what options do I need to not get an objection to what I'm doing here? And (2) is there a better way of achieving this goal, such that I can be compatible with the strict [tt]-ansi -pedantic[/tt] compatibility that I like to use?
Thanks in advance.