pointer

This is a discussion on pointer within the C Programming forums, part of the General Programming Boards category; how would i go about removing the pointers from this function? int process(int *p, int *nchars_ptr, int *nletters_ptr) { static ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    7

    pointer

    how would i go about removing the pointers from this function?

    int process(int *p, int *nchars_ptr, int *nletters_ptr)
    {
    static int cnt = 0, last_char = ' ';

    if(isspace(last_char) && isspace(*p))return 0;if(isalpha(*p))
    {
    ++ * nletters_ptr;if(islower(*p)) *p = toupper(*p);

    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use an array, which is actually a pointer. In other words, you can't. Besides, that's damn ugly code. What exactly is it supposed to do? Give me an example of how you're calling it also. I can pretty much guess what it does, but god, what a nightmare.

    Quzah.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    7
    sorry about the paste job. all this program does is take input and capitalize it and keep track of the number of characters and letters. Lot of code to do something simple.

    if(process(&c, &nchars, &nletters) == 1) putchar(c);


    int process(int *p, int *nchars_ptr, int *nletters_ptr)
    {
    static int cnt = 0, last_char = ' ';

    if(isspace(last_char) && isspace(*p))
    return 0;

    if(isalpha(*p)) {
    ++ * nletters_ptr;
    if(islower(*p)) *p = toupper(*p);
    }
    else if(isspace(*p))
    if(++cnt % NWORDS == 0)
    *p = '\n';
    else *p = ' ';
    ++ * nchars_ptr;
    last_char = *p;
    return 1;
    }

    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Alright. This really is still not enough code to see the full scope of how you want to use it. However, since you want to use multiple values (have them changed) in a single function, you have three options:

    1) use pointers
    2) use a structure and return it
    3) use global variables

    Global is the easiest.
    Pointers are the best.
    Structure is workable.

    Quzah.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 04:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 08:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

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