Reputation: 89
Just started learning c. I'm confused with the pointers and arrays. This is my main function.
int next_statement(char *a, int n);
void consume_char(char c);
int var_lib_check(char type,char var);
int
main(int argc, char *argv[]) {
char statement[MAX_LINE];
int statement_len;
char type[MAX_LINE];
char var[MAX_LINE];
/* Print the output header comment */
printf(OUTPUT_HEADER, argv[0]);
/* Loop through statements read on stdin */
while ((statement_len = next_statement(statement,MAX_LINE)) > 0) {
printf("%s\n",statement);
sscanf(statement,"%s %s",type,var);
var_lib_check(*type,*var);
}
return 0;
int
var_lib_check(char type,char var){
char var_library[MAX_VARS][MAX_LINE];
char new_var[MAX_LINE];
int num_of_var;
int z;
num_of_var = 0;
printf("%s and %s",&type,&var);
if (strcmp(&type,DOUBLE_TYPE)==0||strcmp(&type,INT_TYPE)==0||
strcmp(&type,RTRN_TYPE)==0){
for (z= 0; z < num_of_var; z++){
if (strcmp(var_library[z],&var) == 0){
sprintf(new_var,"x%d",z);
printf("%s %s",&type,new_var);
return z;
}
}
strcpy(var_library[num_of_var],&var);
num_of_var += 1;
sprintf(new_var,"%x%d",num_of_var);
printf("%s %s",&type,new_var);
}
return num_of_var;
}
This program reads the input and if it is either int or double ... it would replace it to be for e.g. int x0.
Why does it only print the first letter of the type and variable when it runs the function when it should print the whole string?
int
next_statement(char *a, int n) {
int c, i;
for (i=0; i < n && (c = getchar()) != EOF; i++) {
if (c == CHAR_SEMI) {
consume_char('\n');
break;
}
a[i] = c;
}
if (c == CHAR_SEMI) {
a[i] = '\0';
return i; /* index when ; was read, so the length of saved. */
}
else if (i >= n) {
printf("%s Line too long.\n", ERROR_PREFIX);
exit(EXIT_FAILURE);
}
return 0;
}
/* reads one char from stdin and errors if it is not what was
* expected, thereby "consuming" the given char.
*/
void
consume_char(char c) {
int x;
if ((x=getchar()) != c) {
printf("%s expected '%c' found '%c'.\n", ERROR_PREFIX, c, x);
exit(EXIT_FAILURE);
}
return;
Upvotes: 0
Views: 2202
Reputation: 7026
You have the function definition as
int var_lib_check(char type,char var){
and you call it as
var_lib_check(*type,*var);
By doing this, you are only passing one character, and not the entire string to it.
You should change your function to
int var_lib_check(char *type,char *var)
and call it as
var_lib_check(type,var);
Now, you are passing the string to it, and you can work on it like a pointer.
Upvotes: 1
Reputation: 53026
It's because you are passing only the first char
to var_lib_check()
. You have to pass the array, so first fix this
var_lib_check(*type,*var);
make it
var_lib_check(type,var);
then fix the var_lib_check()
function
int
var_lib_check(char *type, char *var)
{
char var_library[MAX_VARS][MAX_LINE];
char new_var[MAX_LINE];
int num_of_var;
int z;
num_of_var = 0;
printf("%s and %s", type, var); /* remove the & */
if (strcmp(type, DOUBLE_TYPE) == 0 || strcmp(type, INT_TYPE) == 0 || \
strcmp(type, RTRN_TYPE)==0)
{
for (z = 0 ; z < num_of_var ; z++)
{
if (strcmp(var_library[z], var) == 0)
{
sprintf(new_var, "x%d", z);
printf("%s %s", type, new_var);
return z;
}
}
strcpy(var_library[num_of_var], var);
num_of_var += 1;
sprintf(new_var, "%x%d", num_of_var);
printf("%s %s", type, new_var);
}
return num_of_var;
}
Most of the code invokes undefined behavior, because you pass the address to a char
where a pointer to a string is expected.
A string consists of a sequence of non-nul
bytes followed by a nul
byte, since you where passing the address to just one byte, the functions, namely printf()
and strcmp()
where reading past the value of the passed variable.
Upvotes: 0