The program works i allocate and reallocate data properly.
I wanted to break if c== -1, so i tried to compare c to "-" and data[i-1] == '1' but i got an error. So i tried to just print it to see what comes up.
for (i=0;;i++) {
c=getchar(); /* put input character into c */
if (c== '1' ) { // need to find a way to change it to -1
printf("n-1 is %c",data[i]); // ask
break;
//&& (data[i-1] == '-')
}
The output is
n-1 is ?
*The ? is upside down
Any ideas. Thank you.
Edit:
char *getInput()
{
char *data,*temp;
data= malloc(sizeof(char));
char c; /* c is the current character */
int i; /* i is the counter */
printf ("\n Enter chars and to finish push new line:\n");
for (i=0;;i++) {
c=getchar(); /* put input character into c */
if (c== '1' ) { // need to find a way to change it to -1
printf("n-1 is %c",data[i]); // ask
break;
//&& (data[i-1] == '-')
}
data[i]=c; /* put the character into the data array */
temp= realloc(data,(i+1)*sizeof(char)); /* give the pointer some memory */
if ( temp != NULL ) {
data=temp;
} else {
free(data);
printf("Error allocating memory!\n");
return 0 ;
}
}
data[i]in the code shown. What were you expecting to have printed out?dataallocated? dynamically or statically? Can you show where you take inputs todata?