Jump to content

  • Log In with Google      Sign In   
  • Create Account


Compiler error on Counting how many lines there is?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 MrPhoenix   Members   -  Reputation: 134

Like
0Likes
Like

Posted 20 January 2013 - 04:03 AM

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

 

 



Sponsor:

#2 Bacterius   Crossbones+   -  Reputation: 8152

Like
2Likes
Like

Posted 20 January 2013 - 04:34 AM

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, 20 January 2013 - 04:38 AM.

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#3 MrPhoenix   Members   -  Reputation: 134

Like
0Likes
Like

Posted 20 January 2013 - 04:40 AM

but the application will crash at the first strtok ?



#4 Brother Bob   Moderators   -  Reputation: 7779

Like
2Likes
Like

Posted 20 January 2013 - 04:52 AM

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



#5 swiftcoder   Senior Moderators   -  Reputation: 9584

Like
1Likes
Like

Posted 20 January 2013 - 08:28 AM

strtok modifies the string you pass

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 - Software Engineer @Amazon - [swiftcoding]


#6 Álvaro   Crossbones+   -  Reputation: 11861

Like
3Likes
Like

Posted 20 January 2013 - 09:33 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.

Edited by Álvaro, 20 January 2013 - 09:33 AM.


#7 Khatharr   Crossbones+   -  Reputation: 2818

Like
0Likes
Like

Posted 20 January 2013 - 04:10 PM

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, 20 January 2013 - 04:14 PM.

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.

#8 swiftcoder   Senior Moderators   -  Reputation: 9584

Like
2Likes
Like

Posted 20 January 2013 - 04:13 PM

String literals can be modified. There's a laundry list of reasons why it's not a good idea though.

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 - Software Engineer @Amazon - [swiftcoding]


#9 Khatharr   Crossbones+   -  Reputation: 2818

Like
0Likes
Like

Posted 21 January 2013 - 01:52 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.

#10 SiCrane   Moderators   -  Reputation: 9387

Like
2Likes
Like

Posted 21 January 2013 - 03:11 AM

All the versions of the C++ standard state that attempting to modify a string literal is undefined behavior.

#11 Khatharr   Crossbones+   -  Reputation: 2818

Like
0Likes
Like

Posted 21 January 2013 - 03:38 AM

Nbc_the_more_you_know.jpg


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.

#12 MrPhoenix   Members   -  Reputation: 134

Like
0Likes
Like

Posted 21 January 2013 - 08:19 PM

i finished the problem thanks for all the help :D






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS