Writing a piece of code and failing.

B

bashcommando

Guest
I was writing code and came across an error:
Code:
boot.c:34:12: error: expected string literal before ‘cmd’
The error with the code is in this section:
Code:
while (1)
{
    printf("starfruit>");
    while (test)
    {
        get = getchar();
        if ( get == "\\" ) {
            printf("\n");
            test = 0;
            count = 1;
        } else {
            count = count + 1;
            cmd[count] = get;
        }
    }
    if ( cmd == "halt" ) {
        __asm__("cli");
        __asm__("hlt");
    } else {
        __asm__(cmd);
    }
}
 


Exactly which line is the compiler complaining about in your posted code?

Judging by the fact that the file extension is .c, I am going to assume that you are compiling as C and not C++. In which case, I would guess that the line in question is this one:
Code:
if (cmd == "halt") {
Now, I haven't coded in pure C for a long time (mainly been using C++ for the last 5 or 6 years), so my memory is a little fuzzy in places. But unless the C standard has changed, there never used to be an equality (==) operator defined for C style strings (char arrays).
From what I remember, to test for equality, you need to use a function like strcmp or strncmp to compare the two strings.
 
Exactly which line is the compiler complaining about in your posted code?

Judging by the fact that the file extension is .c, I am going to assume that you are compiling as C and not C++. In which case, I would guess that the line in question is this one:
Code:
if (cmd == "halt") {
Now, I haven't coded in pure C for a long time (mainly been using C++ for the last 5 or 6 years), so my memory is a little fuzzy in places. But unless the C standard has changed, there never used to be an equality (==) operator defined for C style strings (char arrays).
From what I remember, to test for equality, you need to use a function like strcmp or strncmp to compare the two strings.
Code:
__asm__(cmd);
 
cmd is an array - the reference above is "cmd[count] = get;". However, in the line that is complaining, you have "__asm__(cmd);" and above that "if ( cmd == "halt" )".

Shouldn't both of those have cmd[count] or something like that? If this is C, you need to typecast; cmd would be a memory pointer, so if it were a string, it would store where the value starts and how long it is. If it is an array, it stores the structure and the address of the first element (element 0), which is where the length would be stored - not the value and length.


This description is not technically correct - but it should get you to thinking about the data types, which seem mismatched.

And, as JasKinasis mentioned, "==" does not do string compare in C.
 
__asm__("stuff") is a directive to tell the compiler to execute an assembly instruction. The compiler has to specially construct the context around each assembly block. Your program cannot do this at runtime. What you are doing is mixing up a compile-time instruction with runtime ones. You'd have the same sort of problem using pre-processor directives and expecting them to change program flow based on runtime changes.

The compiler is complaining that the __asm__() directive in the else block is expecting a string literal but instead the source has cmd

Google for the GCC-Inline-Assembly-HOWTO
 

Members online


Latest posts

Top