Array of pointers (chars)

This is a discussion on Array of pointers (chars) within the C Programming forums, part of the General Programming Boards category; Hello, I need to make a function that will receive an array of pointers of chars and an char. The ...

  1. #1
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    Array of pointers (chars)

    Hello, I need to make a function that will receive an array of pointers of chars and an char. The function will check how many times the char appears in the array, example:

    Code:
    char *msg[] = {
            "String one",
            "String two",
            "String three",
            "String four",
        };
    Now I pass to the function the array and the char 'e' the function will return me 3.

    Now the question, after that I pass the function, what should I do? I need to check char by char? hmm, I don't need that you make me the whole program, I just need to understand, thanks

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    You could write a function which counts the number of occurrences of a character in a string and pass each string to this function. Checking each character is the easiest way.

  3. #3
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Ok Shiro I get you.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int cama (char *msg, char src);
    
    int main(void)
    {
        int len = 0, i;
        char *msg[] = {
            "String one",
            "String two",
            "String three",
            "String four",
        };
        
        for(i=0;i<5;++i)
            len += cama(*(msg+i),'e');
        
        
        printf("The char 'g' appears %d\n",len);
        getchar();
        return 0;
    }
    
    int cama (char *msg, char src)
    {
        int len = strlen(msg);
        int i,l = 0;
        
        for(i=0; i<len; ++i,++msg)
            if(*msg == src)
              ++l;
        
        return l;
    }
    But it's not better to pass all the array of pointers instead checking string by string or its the same thing?

    Thank you!

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Currently you have the functionality distributed over main and the function. Main() passes each string to the function and the function determines the number of occurrences. You could make your function more general by letting it accept an array of strings. Then you could just pass the array of strings to the function and let the function do all the work.

    I would prefer to let functions do the work and let main() do only a few necessary things, but that's a matter of opinion.

  5. #5
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Ok, I'll change the main function, and let the other function to treat with the strings

    Thank you.

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Forgot one thing to tell, the main reason for making general functions and putting code into functions is reusability of code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. two-dimensional dynamic array of pointers to classes
    By Timo002 in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 07:18 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Array pointers to Multi-Dimensional Arrays
    By MethodMan in forum C Programming
    Replies: 3
    Last Post: 03-18-2003, 09:53 PM
  5. Loading an array using pointers
    By cheesehead in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2001, 05:23 AM

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