Jump to content
  • Advertisement
Sign in to follow this  
Randall Perkins

Compiler error on Counting how many lines there is?

This topic is 2066 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

 

 

Share this post


Link to post
Share on other sites
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).

Edited by Bacterius

Share this post


Link to post
Share on other sites

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);

Share this post


Link to post
Share on other sites

[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.

Share this post


Link to post
Share on other sites
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. Edited by Álvaro

Share this post


Link to post
Share on other sites
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. Edited by Khatharr

Share this post


Link to post
Share on other sites
All the versions of the C++ standard state that attempting to modify a string literal is undefined behavior.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!