Writing a piece of code and failing.

Discussion in 'General Computing' started by bashcommando, Aug 4, 2014.

  1. bashcommando

    bashcommando Active Member

    Messages:
    212
    Likes Received:
    43
    Trophy Points:
    28
    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);
        }
    }


  2. ryanvade

    ryanvade Administrator Staff Member Staff Writer

    Messages:
    1,338
    Likes Received:
    445
    Trophy Points:
    83
  3. JasKinasis

    JasKinasis Member

    Messages:
    69
    Likes Received:
    33
    Trophy Points:
    18
    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.
  4. bashcommando

    bashcommando Active Member

    Messages:
    212
    Likes Received:
    43
    Trophy Points:
    28
    Code:
    __asm__(cmd);
  5. ryanvade

    ryanvade Administrator Staff Member Staff Writer

    Messages:
    1,338
    Likes Received:
    445
    Trophy Points:
    83
  6. bashcommando

    bashcommando Active Member

    Messages:
    212
    Likes Received:
    43
    Trophy Points:
    28
    It shouldn't have to.
  7. unixfish

    unixfish Member

    Messages:
    44
    Likes Received:
    25
    Trophy Points:
    18
    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 Ja****asis mentioned, "==" does not do string compare in C.
    ryanvade likes this.
  8. Ian Norton

    Ian Norton New Member

    Messages:
    1
    Likes Received:
    1
    Trophy Points:
    1
    __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
    MikeyD likes this.

Share This Page