Thread: Could someone please explain why all the alternatives work?

  1. #1
    Registered User Paderi's Avatar
    Join Date
    Apr 2020
    Posts
    68

    Could someone please explain why all the alternatives work?

    I am trying to get a better understanding of pointers.
    Could someone please explain why all the alternatives work?
    And are there any more alternatives?


    Code:
    #include <stdio.h>
    
    
    //float summate(float*);// A
    //float summate(float[]);// B 
    float summate(float z[]);
    
    
    int main()
    {
      float result;
      float a[] = {23.4, 55, 22.6, 3, 40.5, 18};
      result = summate(&a[0]); // could just be summate(a);
      printf("Result = %.2f", result);
      return 0;
    }
    
    
    //float summate(float* x) // C
    float summate(float  x[]) 
    {
      float sum = 0.0;
      for (int i = 0; i < 6; ++i) 
      {
        sum += x[i];
      }
      return sum;
    }

  2. #2
    Registered User Paderi's Avatar
    Join Date
    Apr 2020
    Posts
    68
    Re: Could someone please explain why all the alternatives work?

    What I am asking is, could someone please describe how the alternatives work? What actually happens and why, when the lines, that I have marked, are executed by the system.

    -----

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,404
    Well, first you have to understand that this declares a function named summate having a parameter of type pointer to float, and with a return type of float:
    Code:
    float summate(float z[]);
    Yes, you see what you might recognise as array notation syntax, but here it declares a pointer, not an array.

    This is the same thing, except that it uses what you might recognise as pointer notation syntax, and omits the parameter name:
    Code:
    float summate(float*);
    Since you're only declaring the function without defining it at this point, omitting the parameter name is perfectly fine.

    This is also the same thing, except that it goes back to what you might recognise as array notation syntax, but omitting the parameter name, which as before is permitted:
    Code:
    float summate(float[]);
    This comment here is absolutely correct because in this context, the array would be implicitly converted to a pointer to its first element, which is of course another way of saying &a[0]:
    Code:
    result = summate(&a[0]); // could just be summate(a);
    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

  4. #4
    Registered User Paderi's Avatar
    Join Date
    Apr 2020
    Posts
    68
    This is my knowledge no more, no less.
    IF: int a, *p; a = 20; p = &a;
    printf("p : %x\n", p ); // shows address
    printf("*p : %d\n", *p ); //shows a
    The value of a pointer (p = &a) is the address of another variable
    p* shows the value of another variable by means of its address

    ----

    now what is (int *) ?
    now what is (int[]) ? how am I to understand that a pointer is being declared? A pointer to what?
    Last edited by Paderi; 12-20-2021 at 07:07 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,404
    Quote Originally Posted by Paderi
    now what is (int *) ?
    This doesn't appear in the context you provided. I'm guessing that you're asking this question in the context of your post #1, for example, you're asking what does (int *) mean in this declaration:
    Code:
    void foo(int *);
    The answer is that it is a parameter type list that declares that the function named foo has a parameter of type pointer to int.

    Quote Originally Posted by Paderi
    now what is (int[]) ?
    Again, I'm assuming that you're asking something along the lines of what does (int[]) mean in this declaration:
    Code:
    void foo(int[]);
    The answer is that it is a parameter type list that declares that the function named foo has a parameter of type pointer to int. This particular syntax tends to be used when dealing with an array, but this is not required (i.e., you can use the int* syntax instead, and the caller doesn't have to pass an array).

    Quote Originally Posted by Paderi
    how am I to understand that a pointer is being declared?
    You have to understand that in this context, i.e., when you see this syntax used in a parameter type list, it means that the function has a pointer parameter. You understand it by memorisation, by looking at examples and exaplanations, etc, because you're learning the syntax and semantics of a language. For example, how are you to understand what the English word "understand" means? You could look up a definition of "understand" in an English dictionary, possibly in conjunction with sentence examples in which "understand" is used (which might be provided by the dictionary).

    Quote Originally Posted by Paderi
    A pointer to what?
    A pointer to whatever the argument of the function points to. Look back at your post #1: in the function call summate(&a[0]), &a[0] is the argument to summate. Therefore, the parameter named x points to a[0] for that function call. In the function's forward declaration, the same parameter is named z, which is confusing, so don't do that: keep the parameter names consistent.
    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

  6. #6
    Registered User Paderi's Avatar
    Join Date
    Apr 2020
    Posts
    68
    Laserlight, I am immensely grateful for your response. I need a few things out of the way before I can digest it. But prior to that:

    Code:
    int main()
    Code:
    {
      float result;
      float a[] = {23.4, 55, 22.6, 3, 40.5, 18};
      result = summate(&a[0]); // could just besummate(a);
      printf("Result = %.2f", result);
      return 0;
    }
    

    in
    summate(a); is 'a' a pointer to array a?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,404
    No, a is an array of 6 float elements. But when it is passed as an argument to summate, it is converted to a pointer to its first element.
    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

  8. #8
    Registered User Paderi's Avatar
    Join Date
    Apr 2020
    Posts
    68
    Laserlight, I would like to thank you very much for your insight giving responses!

  9. #9
    Registered User Paderi's Avatar
    Join Date
    Apr 2020
    Posts
    68
    In the code example where I show alternative parameters (that all work), the parameter is a pointer.
    In "summate(a)" the 'a' is a pointer to the first element of 'array a'. But "&a[0]"will also work as the parameter of the function.
    How can an an "address of the first element" and a "pointer to the first element" effectively be the same thing?

  10. #10
    Registered User Paderi's Avatar
    Join Date
    Apr 2020
    Posts
    68
    "No, a is an array of 6 float elements. But when it is passed as an argument to summate, it is converted to a pointer to its first element." ---> When I park my car where it is not allowed and a policeman approches it to leave a fine under its windscreen wiper, it is converted into a bicycle.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,404
    Quote Originally Posted by Paderi
    How can an an "address of the first element" and a "pointer to the first element" effectively be the same thing?
    The result of the address-of operator is a pointer. In post #4, you wrote that "The value of a pointer (p = &a) is the address of another variable", so you should already understand this.

    Quote Originally Posted by Paderi
    When I park my car where it is not allowed and a policeman approches it to leave a fine under its windscreen wiper, it is converted into a bicycle.
    Yes, of course. Why do you say the obvious?
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Here's To The Alternatives
    By SMurf in forum General Discussions
    Replies: 3
    Last Post: 10-04-2013, 10:33 AM
  2. Replies: 8
    Last Post: 12-16-2012, 08:42 AM
  3. std::map Alternatives
    By EvilGuru in forum C++ Programming
    Replies: 8
    Last Post: 04-12-2008, 03:45 AM
  4. How does strtok work? Please explain..
    By aspand in forum C Programming
    Replies: 4
    Last Post: 05-13-2002, 07:29 PM

Tags for this Thread