Writing a piece of code and failing.

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

  1. bashcommando

    bashcommando Active Member

    Joined:
    Jan 28, 2014
    Messages:
    223
    Likes Received:
    49
    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

    Joined:
    Aug 4, 2012
    Messages:
    1,863
    Likes Received:
    655
  3. JasKinasis

    JasKinasis Active Member

    Joined:
    Jun 17, 2014
    Messages:
    199
    Likes Received:
    118
    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

    Joined:
    Jan 28, 2014
    Messages:
    223
    Likes Received:
    49
    Code:
    __asm__(cmd);
     
  5. ryanvade

    ryanvade Administrator Staff Member Staff Writer

    Joined:
    Aug 4, 2012
    Messages:
    1,863
    Likes Received:
    655
  6. bashcommando

    bashcommando Active Member

    Joined:
    Jan 28, 2014
    Messages:
    223
    Likes Received:
    49
    It shouldn't have to.
     
  7. unixfish

    unixfish Member

    Joined:
    Jan 8, 2014
    Messages:
    93
    Likes Received:
    50
    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.
     
    ryanvade likes this.
  8. Ian Norton

    Ian Norton New Member

    Joined:
    Aug 6, 2014
    Messages:
    1
    Likes Received:
    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