Compiler error on Counting how many lines there is?

Started by
10 comments, last by Randall Perkins 11 years, 2 months ago

This is the function


int count_lines(char *text) {
    printf("Testing count lines :D \n");
    char *result;
    int return_lines = 0;
    result = strtok(text,"\n");
    while(result != NULL) {
        return_lines+= 1;
        result = strtok(NULL,"\n");
    }
    return return_lines;
}

Example how i use the function


int main(){
    int lines = count_lines("This is a simple game lets enjoy this\nIt breaks a new era in to games\nwhahahaha zoom in");
    printf("Line Count : %i \n", lines);
    return 0;
}

Compiler Errors


In function 'count_lines':
warning: implicit declaration of function 'strtok'
warning: assignment makes pointer from integer without a cast
warning: assignment makes pointer from integer without a cast

Advertisement

Those are actually warnings, not errors. And it means the compiler doesn't know what 'strtok' is, and because this is C, all undeclared function prototypes implicitly default to a prototype which returns int (resulting in those incorrect assignments, since the actual function was meant to return a char*).

The solution is to find the correct header for strtok. A quick google search tells me it's "string.h", so make sure you have included this file in your project, with:


#include <string.h>

Also, what are your compiler settings? Some particular combinations may produce false positives, in particular if you request extensions from the compiler via command-line argument, but don't set the appropriate macro, and others will just plain not work until you tell the compiler you want such and such function (if it's non-standard).

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

but the application will crash at the first strtok ?

strtok modifies the string you pass, but you're passing a string literal which cannot be modified. You have to allocate memory for the string, either dynamically or statically.

char string[] = "This is a simple game lets enjoy this\nIt breaks a new era in to games\nwhahahaha zoom in"; int lines = count_lines(string);

[quote name='Brother Bob' timestamp='1358679138' post='5023455']
strtok modifies the string you pass[/quote]

Which fact should suggest that strtok() is not necessarily a good way to accomplish this task. I'd suggest that a naive loop over the original string, counting each '\n' as you encounter it, would be a better solution.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

int count_lines(char const *s) {
  int result = 0;

  for (; *s; ++s) 
    result += *s == '\n';
    
  return result;
}

Notice the `const' in the parameter type. This means that it is OK to call this function on string literals, since I promise I won't change anything.
strtok modifies the string you pass, but you're passing a string literal which cannot be modified.
String literals can be modified. There's a laundry list of reasons why it's not a good idea though.

Edit - Ah, I should qualify that. You'd have to have module page write permission to modify it. Forgot that part.
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

[quote name='Khatharr' timestamp='1358719824' post='5023646']
String literals can be modified. There's a laundry list of reasons why it's not a good idea though.[/quote]

String literals are specifically not writable.

It may depend on the particular implementation whether or not string literals are actually stored in read-only memory, however.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Oh? This I have not seen. Is this C specific or does it apply to C++ as well?
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.
All the versions of the C++ standard state that attempting to modify a string literal is undefined behavior.

This topic is closed to new replies.

Advertisement