Thread: pointer help

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    1

    pointer help

    Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.
    exercise 1-9 chapter 1 from the c programming language K&R.

    i can do this program generally..i was wondering if it could be done through pointers...i tried but couldnt do....
    plz help or give a hint..!!
    thnkz

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    What have you tried?

    If you were stuck from the beginning with the use of pointers, show us the version of the program that you did without using pointers (or at least you think you didn't use pointers).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User piyush.sharma's Avatar
    Join Date
    Aug 2012
    Location
    Noida, India
    Posts
    9
    Its seems your homework. By the way, strings and its functions work with pointers. So if you have done it, you used pointers. To do this explicitly, (If you store input in a variable) initialize two pointer with the string.
    Code:
    void reFormatString(char *str)
    {
            char *tmp,*t,*t1;
            tmp = (char *) malloc(strlen(str));
            t1 = tmp;
            t = str;
            while(*t)
            {
                    if(*t == ' ' && *(t+1) == ' ')
                    {
                            while(*(t+1) == ' ')
                                    t++;
    
                    } 
                    *t1 = *t;
                    t1++;
                    t++;
            }
            *t1=0;
            strcpy(str,tmp);
            free(tmp);
    }
    Now improve this function to get your work done.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Stop spoon feeding solutions to newbies that provide no code.

    Also, don't cast malloc.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User piyush.sharma's Avatar
    Join Date
    Aug 2012
    Location
    Noida, India
    Posts
    9
    Ok !!! I will take care of it. Thanks to interrupt.

  6. #6
    Registered User piyush.sharma's Avatar
    Join Date
    Aug 2012
    Location
    Noida, India
    Posts
    9
    Quote Originally Posted by claudiu View Post
    Also, don't cast malloc.
    Whats the issue if I cast malloc(). Mostly I have seen in examples, people cast the return value of malloc() because it returns (void *).

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    The compiler will not give an error when you forget to #include <stdlib.h>

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    69
    Quote Originally Posted by claudiu View Post
    Stop spoon feeding solutions to newbies that provide no code.

    Also, don't cast malloc.
    Stop getting your panties in a bunch, he was giving him some example code. It's not like the original poster wanted a code written for him for a specific task, it was for an exercise in his book. I see no problem with posting some code for someone to see how it is implemented.

    Now, as for casting malloc... You need to cast malloc if you want it to compile in both C and C++... It doesn't hurt anything, just makes it compile in C++.

    EDIT: I don't mean this as a flame, I was just merely voicing my opinions. Sorry if this sounds a little insulting. <3
    Last edited by inu11byte; 08-07-2012 at 04:29 AM.

  9. #9
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by inu11byte View Post
    Stop getting your panties in a bunch, he was giving him some example code. It's not like the original poster wanted a code written for him for a specific task, it was for an exercise in his book. I see no problem with posting some code for someone to see how it is implemented.

    Now, as for casting malloc... You need to cast malloc if you want it to compile in both C and C++... It doesn't hurt anything, just makes it compile in C++.

    EDIT: I don't mean this as a flame, I was just merely voicing my opinions. Sorry if this sounds a little insulting. <3
    If you want to compile in C++ you might as well drop all the "C-style" and write C++ code. Use strings instead of char arrays, use references in functions instead of passing pointers and allocate memory with new rather than malloc and free it with delete rather than free.

    If, however you come to the C forum for advice you will receive the "C recipe" of doing things.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by inu11byte View Post
    Stop getting your panties in a bunch, he was giving him some example code. It's not like the original poster wanted a code written for him for a specific task, it was for an exercise in his book. I see no problem with posting some code for someone to see how it is implemented.
    Untwist your own panties before telling others off.

    Unfortunately, the purpose of exercises in a book is that the student will learn more by doing them, than by being given a solution. Anyone who is experienced with helping people learn will give pointers, but not solve the problem. Doing so actually does a disservice, because it reduces opportunities to develop problem-solving skills.

    Quote Originally Posted by inu11byte View Post
    Now, as for casting malloc... You need to cast malloc if you want it to compile in both C and C++... It doesn't hurt anything, just makes it compile in C++.
    Except for the fact that usage of malloc() in C++, whether cast or not, is often bad practice, because malloc() doesn't play well with other parts of C++. There is a reason that C++ has operators new and delete in place of malloc().

    In C, casting malloc() is considered bad practice because it hides other severe mistakes by the programmer.

    So you're wrong whichever way you look at the problem.

    Quote Originally Posted by inu11byte View Post
    EDIT: I don't mean this as a flame, I was just merely voicing my opinions. Sorry if this sounds a little insulting. <3
    Well, you need to think about how you word your opinions. There is nothing wrong with expressing disagreement, if you justify your position.

    However, you have chosen to express views about things that experienced people KNOW more than you about. Your tone to claudiu was insulting, which is why I decided to be less than cordial with you.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  11. #11
    Registered User piyush.sharma's Avatar
    Join Date
    Aug 2012
    Location
    Noida, India
    Posts
    9
    Quote Originally Posted by grumpy View Post
    the purpose of exercises in a book is that the student will learn more by doing them, than by being given a solution. Anyone who is experienced with helping people learn will give pointers, but not solve the problem. Doing so actually does a disservice, because it reduces opportunities to develop problem-solving skills.
    I Agree, but i felt tough to express that code in words so I paste that one. I didn't find an option to delete that post to end that mistake.
    Its sound bad and not a good impression if we write text in a way as inu11byte wrote. Please take care of this guys.

  12. #12
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    In C, casting malloc() is considered bad practice because it hides other severe mistakes by the programmer.


    What severe mistakes would they be?

  13. #13
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I think this post describes the problem with a short humorous anecdote.

    Why casting malloc is a bad idea...
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 21
    Last Post: 01-22-2012, 11:35 AM
  2. Replies: 3
    Last Post: 10-30-2009, 04:41 PM
  3. Replies: 9
    Last Post: 06-13-2009, 02:31 AM
  4. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  5. Replies: 4
    Last Post: 08-27-2007, 11:51 PM

Tags for this Thread