Char Array passing help

This is a discussion on Char Array passing help within the C Programming forums, part of the General Programming Boards category; Hello everyone Im working on a function that takes a character array and returns the nth word in the array ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    29

    Char Array passing help

    Hello everyone
    Im working on a function that takes a character array and returns the nth word in the array into another array using pointers. But Soooomehow, its not working... sigh...
    It would be very much appreciated if someone can tell me what im doing wrong
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char *sbreak(const char *st, char *rst, int y)
    {
      int x = 0;
      int rli = 0;
      int n = strlen(st);
      for(int i = 0; i < n; i++)
        {
          printf("%c",st[i]);
        }
      for(int i = 0; i < n; i++)
        {
          if(st[i] == " ")
    	{
    	  x++;
    	  continue;
    	}
          if(x == y)
    	{
    	  rst[rli] = st[i];
    	  rli++;
    	}
        }
    }
          
    int main(int argc, char *argv[])
    {
      char stbreak[128];
      fgets(stbreak, 100, stdin);
      char rst[128]; //the string that is being written to
      sbreak(stbreak, rst, 2); //2 denotes that the 3rd word will be read
      int a = strlen(rst);
      for(int i = 0; i < a; i++)
        {
          printf("%c",rst[i]);
        }
      return 0;
    }
    Thanks!
    (by the way, im using gcc in emacs to compile)
    Last edited by Whyrusleeping; 05-23-2011 at 11:23 PM. Reason: code fix

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    Compile with warnings, and you'll see some things to fix.
    Code:
    $ gcc -Wall -std=c99 bar.c
    bar.c: In function ‘sbreak’:
    bar.c:16: warning: comparison between pointer and integer
    bar.c:16: warning: comparison with string literal results in unspecified behavior
    bar.c: In function ‘main’:
    bar.c:35: error: ‘a’ undeclared (first use in this function)
    bar.c:35: error: (Each undeclared identifier is reported only once
    bar.c:35: error: for each function it appears in.)
    bar.c:38: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2009
    Location
    Look in your attic, I am there...
    Posts
    92
    You shouldn't be using continues unless they are necessary, also instead of using strlen you could use just check for the ascii null '\0'. Look at my example for a reference:

    Code:
    /* Compile with gcc -std=c99 File_Name.c -o File_Name.exe */
    
    #include <stdio.h>
    
    char *Grab_Word(
      const char *String,
      const int  Word_Number
    ){
      /* Test for a word number less than 1 */
      if(Word_Number < 1)
        return NULL;
    
      /* Stores the index of the String where the word starts */
      int i = 0;
      
      /* Loop through the unneeded words */
      for(int j = 0;j < Word_Number - 1;j++){
      
        /* If the string is an escape character return NULL -- there were not that
           many number of words in the string */
        if((String[i] < ' ' && String[i] > '\t') || String[i] < '\t')
          return NULL;
          
        /* Loop through the spaces */
        for(;String[i] != '\0' && (String[i] == ' ' || String[i] == '\t');i++);
        
        /* Loop through the characters */
        for(;String[i] != '\0' && String[i] > ' ';i++);
      }
      
      /* All we did already was loop passed the words we didnt want,
         there is still a chance that there is no next word (that the String just 
         has spaces at the end. So we loop through the spaces again (just in case) */
      for(;String[i] != '\0' && (String[i] == ' ' || String[i] == '\t');i++);
      
      /* We test for a null character (again, just in case) */
      if((String[i] < ' ' && String[i] > '\t') || String[i] < '\t')
        return NULL;
      
      /* This can be tricky if you are new to c. What I am doing here is
          getting the pointer of the element i (where the word starts)
          and returning it. I am also explicitly type casing the address
          to type char* for the compiler warnings, but without it is
          is still a pointer to the start of the word */
      return (char *)&String[i];
    }
    
    int main(void){
      printf("%s\n", Grab_Word("This is a test. It was a SUCCESS!", 5));
    }
    EDIT: All my code did was return the char * to the start of the word needed, not exactly what you were looking for. Sorry if I confused you.
    Last edited by 127.0.0.1; 05-23-2011 at 11:50 PM.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    29
    What exactly happens when you pass back the pointer, are you just passing back one letter? or the rest of the phrase?
    Last edited by Whyrusleeping; 05-23-2011 at 11:32 PM.

  5. #5
    Registered User
    Join Date
    May 2009
    Location
    Look in your attic, I am there...
    Posts
    92
    Quote Originally Posted by Whyrusleeping View Post
    Okay, ive fixed the stupid errors, but im still getting the 'comparison between pointer and integer' error. im comparing an element in an array of characters to the space character. am i doing something wrong (well obviously i am but i dont know what it is) ?
    Can you post the code in question?

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    29
    Code:
     
    if (st[i] == " ")  // this is the line that its having an error with
    {
    x++;
    continue;
    }

  7. #7
    Registered User
    Join Date
    May 2009
    Location
    Look in your attic, I am there...
    Posts
    92
    An element of a char array is a char not a string. Using a double quote denotes a string i.e " ", using a single quote denotes a character ' '.

    This should fix it:
    Code:
     
    if (st[i] == ' ')  // this is the line that its having an error with
    {
    x++;
    continue;
    }
    EDIT: Also, if you want to print a string you can use printf("%s", String) instead of looping through the characters, just make sure your string is null terminated
    Last edited by 127.0.0.1; 05-23-2011 at 11:38 PM.

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    29
    It works perfectly now. I think i love you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing 2d char array to a function
    By synhyborex in forum C Programming
    Replies: 5
    Last Post: 02-16-2011, 04:43 PM
  2. Passing char as an array?
    By xaykogeki in forum C Programming
    Replies: 2
    Last Post: 04-18-2010, 11:54 PM
  3. Passing char array to fuction
    By taurus in forum C Programming
    Replies: 12
    Last Post: 10-09-2009, 07:07 PM
  4. Passing a char * into char array
    By Bladactania in forum C Programming
    Replies: 4
    Last Post: 02-20-2009, 11:33 AM
  5. Passing char array into function
    By Crysist in forum C++ Programming
    Replies: 2
    Last Post: 01-07-2005, 12:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21