Haythem Tarchouna
Haythem Tarchouna

Reputation: 17

Reversing string using stack error: incompatible type for argument 1 of 'top'|

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct item{
    char value;
    struct item* next;
}item;

typedef struct{
    item *top;
}stack;

item* push_item(item* head,char val){
    item *temp=(item*)malloc(sizeof(item));
    temp->value=val;
    temp->next=head;
    head=temp;
    return head;
}
void push(stack*a,char val){
    a->top=push_item(a->top,val);
}
void print_stack(stack a){
    while(a.top!=NULL){
        printf("%c",a.top->value);
        a.top = a.top->next;
    }
}
item* top(stack a){
return a.top;
}
item* pop_item(item* head){
    if( head==NULL){
        printf("Stack is Empty");
        return head;
    }
    else{
        item *toDel=head;
        head=head->next;
        free(toDel);

    }
    return head;
}
void pop(stack *a){
    if (a!=NULL){
        a->top=pop_item(a->top);
    }
}


void main(){
    stack a;
    int i;
    a.top=NULL;
    char st[10];
    printf("Dooner la chaine= ");
    scanf("%s",st);
    for(i=0;i<strlen(st);i++){
        push(&a,st[i]);
    }
    strcpy(st,"");
    for(int a=0;a<i;a++){
    strcat(st,top(a)->value);
         pop(&a);
    }
}

I'm trying to reverse a string using a stack. The push function is working and I can even print the items in reverse order if I directly print the stack.

When I try to change the initial string to the new string I get this error:

incompatible type for argument 1 of 'top'|" at this instruction " strcat(st,top(a)->value);

Upvotes: 0

Views: 98

Answers (2)

samuelnj
samuelnj

Reputation: 1637

You're going to end up with a problem in this particular case because

  1. you re-declare the variable a in the for loop with type int which over rules the previous definition of a and gives you the error of an invalid argument.

  2. you're passing the wrong argument type to strcat top(a)->value has type char, but strcat accepts char *

  3. Even if you change it to type char * (by say doing &(top(a)->value) ) you'll have a problem because top(a)->value is a single character that is not null terminated, and strcat will not know when to stop copying from the source

What you probably should do is just access st via the array eg st[i] = top(a)->value and then null terminate it once you exit the for loop.

Upvotes: 1

medalib
medalib

Reputation: 937

First off all in your for loop, you are redeclaring a as int, which will be shadowing variable a declared as stack in main. So change a to something else:

for(int j = 0; j < i; j++)

As you are storing one character per node, strcat won't work for you as it expecting a char * as an argument:

 strcat(st,top(a)->value);
           ^
           char * is expected but argument is of type char.     

As top(a)->value will be the stored character in the node, you can assign it directly to your string:

int j;
for(j = 0; j < i; j++){
     st[j] = top(a)->value;
     pop(&a);
}
st[j] = '\0'; /* Don't forget to null-terminate your string. */

Upvotes: 1

Related Questions