Thread: Newbie function return question

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    1

    [SOLVED] Newbie function return question

    Hello fellow programmers,

    I am way new to C programming, but not programming itself. Been coding PHP for quite a while. C seems to be a very promising language, therefore I would like to get my deeper sight in it.

    However I have came up with a problem that I cannot figure how to solve. I bet it is a easy task to do, somehow I just can't handle it on my own.

    I am posting a shortened version of my code, it's obvious that you don't need it all.

    Here is the main program:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include "spaces.h"
    
    int main()
    {
        char *tekstas = file_get_contents("duomenys.txt");   
        int *spaces = countspaces(tekstas);
        return 0;
    }
    and here is a shortened version of spaces.h
    Code:
    int *countspaces(char *buff)
    {
        int i;
        int spaces = 0;
        for(i = 0; i < strlen(buff)-1; ++i)
        {
        	if(isspace(buff[i]) && isspace(buff[i+1]))
        	{
       			spaces++;
        	}
        }
        return spaces;
    }
    When my program is using countspaces function I get the following warning: <...>spaces.h [Warning] return makes pointer from integer without a cast

    Can anyone tell me how do I solve this warning and explain why (so that I wouldn't have this question again)?

    Thank you in advice,
    Faifas

    Edit: I have solved this issue. All I had to do is remove pointers, so the fixed code is the following:

    main program:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include "spaces.h"
    
    int main()
    {
        char *tekstas = file_get_contents("duomenys.txt");   
        int spaces = countspaces(tekstas); // no pointer
        return 0;
    }
    and spaces.h is now:
    Code:
    int countspaces(char *buff) // no pointer also
    {
        int i;
        int spaces = 0;
        for(i = 0; i < strlen(buff)-1; ++i)
        {
        	if(isspace(buff[i]) && isspace(buff[i+1]))
        	{
       			spaces++;
        	}
        }
        return spaces;
    }
    Last edited by faifas; 06-29-2009 at 05:09 AM. Reason: Solved.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Just a suggestion for you:
    Code:
    for(i = 0; i < strlen(buff)-1; ++i)
    This code must compute the length of the string every time through the loop. It would be better to store the length of the string in a variable, and then use that variable as the condition in your loop.

  3. #3
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    I think your program is counting spaces that have other spaces right after them. Is that what you want?
    Last edited by Zlatko; 06-29-2009 at 10:23 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

Tags for this Thread