Thread: Help on simple pointer program

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    19

    Help on simple pointer program

    I'm not asking anybody to write the program. All I need is a poke in the right direction or some hints. I'm so lost, I don't know if I'm even in the right ballpark right now. Here is the question:

    Write a function that takes a single string as its argument and returns a pointer to the first nonwhite character in the string.

    Here is what I have right now:
    Code:
    #include <stdio.h>
    
    char * point(char s[])
    {
        int i;
        char *s_pointer;
        for (i = 0; s[i] != ' ' && s[i] != '\0'; i++)
        {
            s_pointer = &s;
        }
        return s_pointer;
    }
    
    int main()
    {
        char s[] = "Four score";
        char z = *point(s);
        printf("%s", z);
        return 0;
    }
    Am I anywhere near the right answer? Please help!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by KrazySocoKid View Post
    Code:
    #include <stdio.h>
    
    char * point(char s[])
    {
        int i;
        char *s_pointer;
        for (i = 0; s[i] != ' ' && s[i] != '\0'; i++)
        {
            s_pointer = &s;
    s never changes, so that last line does the same thing no matter what spot you are in your array. You should be doing &s[ i ] so that you assign the address of that spot in the string. Also, if the first letter isn't a space, as in your example:
    Quote Originally Posted by KrazySocoKid View Post
    Code:
    int main()
    {
        char s[] = "Four score";
    Then s_pointer will be used uninitialized.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    19
    Thank you very much!

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    19
    When I run the program I'm getting the output as r. It should be F? Any idea what's going on?

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Ignoring the check for \0 since that's not a problem, your loop continues while the character isn't a space. F isn't a space, neither is o, u or r. The first space is. So that points out two problems - your loop has the wrong exit criteria and you have an off-by-one error in the character you return.

    Also look into the isspace() function instead of comparing against ' '.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    19
    The isspace() function is not in the stdio.h library right? Which library is it in?

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    This is where it's a HUGE help to have help files for your compiler... the library documentation is 1/2 of the toolkit.

    It's in ctype.h

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    19
    Code:
     #include  #include   char * point(char s[]) { 	int i; 	char *s_pointer; 	for (i = 0; s[i] != isspace(*(s_pointer)); i++) 	{ 		s_pointer = &s[i]; 	} 	return s_pointer; }  int main() { 	char s[] = " Four score"; 	int z = *point(s); 	printf("%c\n", (char)z); 	return 0; }

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    19
    I'm sorry I have no idea why my code is posting sideways

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Because the file you are copy/pasting from doesn't have newlines in it...

    If you are copying from the forum to re-paste, click the "to plain text" button first.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple program, simple problem
    By KAUFMANN in forum C Programming
    Replies: 5
    Last Post: 02-16-2011, 01:16 PM
  2. simple pointer
    By cheeta in forum C Programming
    Replies: 3
    Last Post: 04-28-2010, 12:05 PM
  3. Simple Pointer Program
    By eater in forum C Programming
    Replies: 3
    Last Post: 05-28-2009, 11:35 AM
  4. Simple program...simple problem?
    By deadherorising in forum C Programming
    Replies: 2
    Last Post: 03-12-2009, 08:37 PM
  5. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM