Thread: Very stupid question about pointers

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    16

    Very stupid question about pointers

    Hi, i just want to make sure, Is this correct?

    Code:
    int f1(char *a)
    {
          return f2(&a);
    }
    
    int f2(char *a)
    {
           *a++;
           return 1:
    }
    
    or should be;
    
    int f1(char *a)
    {
          return f2(*a);
    }

    Thank you!!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Think: in f1, a is a char*. Therefore, &a is a char**, and *a is a char. For f2, the parameter a is a char*. Therefore, what should you pass to f2 from f1?
    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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well did you try them both with a compiler (which would have taken mere seconds)

    Both are wrong by the way.
    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.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    Hmmm .. thats true.. so i would just have to make like this?Yea thats makes sense...

    Code:
    int f1(char *a)
    {
          return f2(a);
    }
    I tryed to make that like here:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int f2(char *a);
    int f1(char *a);
    
    
    int main()
    {
        char a;
        int b;
            
        a = 100;
        
        b = f1(&a);
           
       printf("%u \n", a);
    
        
        system("PAUSE"); 
    }
    
    int f1(char *a)
    {
          return f2(a);
    }
    
    int f2(char *a)
    {
           *a++;
           return 1;
    }
    But the output is 100.. whats wrong?

    i did try with a++ but with the same output...

    Thank you!

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Try with (*a)++

    It´s a precedence thing.

    Also, if you´re using gcc, then use "-W -Wall" as command line options.
    IIRC, it would complain that your *a++ was a "unused" side effect.
    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.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    Thank you!! It worked You helped a lot!! Actually, it did not complain haha. But thank you.

    And laserlight made everything more clear to me!!

    Cya

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    -pedantic -O2

    Optimisation implies data flow analysis, which might be needed for more advanced unused detection.
    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.

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    Oh, ok i will make use of that!! Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-04-2005, 12:17 AM
  2. Stupid Question
    By digdug4life in forum C++ Programming
    Replies: 22
    Last Post: 05-17-2005, 11:43 AM
  3. Very stupid question involving pointers
    By 7smurfs in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 06:15 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. stupid, stupid question
    By xelitex in forum C++ Programming
    Replies: 5
    Last Post: 12-22-2004, 08:22 PM