confused with a char array problem

This is a discussion on confused with a char array problem within the C++ Programming forums, part of the General Programming Boards category; Here's the problem Write a function that will return a pointer to a character, specified by the user of the ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    25

    confused with a char array problem

    Here's the problem
    Write a function that will return a pointer to a character, specified by the user of the function, within a character array. The array and the desired character are to be the only two inputs to the function.

    I am confused with the purpose of the problem. Does it ask for the pointer address or the character? Does there exist more than one data structures?

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    it wants a pointer to the address that the character can be found at...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    The "purpose" of the problem is to understand functions and pointers.

    The "purpose" of the function would be to check a string for a character. Let's say you let a user input a string and you want to see if it has a space in it. How could you use your function to tell you that?
    Sent from my iPadŽ

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    25
    it should be a simple function, however I am confused.
    Anybody gives a sample function for the problem?

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Subtle...
    Sent from my iPadŽ

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    the prototype should look like this

    Code:
    char * findchar( char str[],  char what );
    Kurt

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    25
    Is this the right solution?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    #include <iomanip>
    
    char * loot_for_char( char str[],  char a )
    {
        int val = 0;
        char *pt;
        while ((str[val] != a)&&(str[val] != '\0')) val++;
        if (str[val] != '\0') pt = &str[val];
        else  pt = NULL;
        return pt;
    
    }
    
    int main()
    {
          char str1[100];
          char ch1;
          int pos = 0;
    
          cout<<"Give me the string: "<<endl;
          cin>>str1;
          cout<<"Give me the character: "<<endl;
          cin>>ch1;
          cout<<"the pointer is to: "<< *(findchar (str1,ch1))<<endl; 
          system("PAUSE");
          return 0;
    }

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    Code:
       if (str[val] != '\0') pt = &str[val];
    There is no need for &, and the while loop can probably be optimized to
    Code:
    for (int val(0); (str[val] != a)&&(str[val] != '\0'); ++val)

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    A bunch of problems there. First off, you never declare the use of the std namespace. You shouldn't read a string in with the >> operator. You should use cin.getline(). Secondly, if you had even attempted to compile this, you would have known you're calling your function with the wrong identifier. What the hell is "findchar", you called it "loot_for_char". You also, as I said before, shouldn't use system(), use cin.get() to pause.

    Lastly, you can really cut down that function a whole lot by eliminating that temp pointer all together. Consider that your str[val] already points to null if it doesn't point to the correct character.
    Sent from my iPadŽ

  10. #10
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by citizen
    Code:
       if (str[val] != '\0') pt = &str[val];
    There is no need for &,
    Yes there is to make it a char *
    Kurt
    EDIT: and no there isn't if you do it this way
    Code:
       if (str[val] != '\0') pt = str+val;
    Last edited by ZuK; 05-01-2006 at 03:55 AM.

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    25
    Quote Originally Posted by SlyMaelstrom
    A bunch of problems there. First off, you never declare the use of the std namespace. You shouldn't read a string in with the >> operator. You should use cin.getline(). Secondly, if you had even attempted to compile this, you would have known you're calling your function with the wrong identifier. What the hell is "findchar", you called it "loot_for_char". You also, as I said before, shouldn't use system(), use cin.get() to pause.

    Lastly, you can really cut down that function a whole lot by eliminating that temp pointer all together. Consider that your str[val] already points to null if it doesn't point to the correct character.
    Thanks for the comment,but what's difference if using cin.getline()? and replacing system() by cin.get()?
    here's a recursive version, any suggestion for this one?

    Code:
    char * look_for_char (char str[],  char ch1, int val )
    {
        char *ptr;
        if ((str[val] != ch1)&&(str[val] != '\0')) {val++; return look_for_char (str, ch1,val); }
        else return &str[val];
    }

  12. #12
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    operator>> stops at whitespace. Which means if I put in the string "Hello World", I'll end up getting "Hello" in the variable and " World" will still be in the input buffer.
    Sent from my iPadŽ

  13. #13
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    system() calls -- you guessed it, the system. If I rigged your box to call "virus_0081.bat" upon every "pause" command invocation, well...

    And what if I built your code on a microwave oven processor which happens to *not* have the "pause" system call?

    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  14. #14
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by jafet
    And what if I built your code on a microwave oven processor which happens to *not* have the "pause" system call?
    or linux. or mac.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  15. #15
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by major_small
    or linux. or mac.
    Somehow I'm already used to that
    Code:
    bash: pause: command not found
    when trying to run sample code from messageboards. Still makes me angry...
    Kurt

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  3. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  4. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 09:40 PM
  5. help with array of char and char **
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 04-20-2002, 02:23 PM

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