Very stupid question about pointers

This is a discussion on Very stupid question about pointers within the C Programming forums, part of the General Programming Boards category; Hi, i just want to make sure, Is this correct? Code: int f1(char *a) { return f2(&a); } int f2(char ...

  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
    21,636
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    -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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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-03-2005, 11:17 PM
  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, 05:15 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. stupid, stupid question
    By xelitex in forum C++ Programming
    Replies: 5
    Last Post: 12-22-2004, 07:22 PM

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