Thread: Can someone explain, why the programm is throwing 2 warning when compiling?

  1. #1
    Registered User
    Join Date
    Nov 2022
    Posts
    4

    Can someone explain, why the programm is throwing 2 warning when compiling?

    Hey, i am completely new to C programming, learning it for my Uni right now, my C exercise group and me have absolutely no clue, why those warnings are thrown, when compiling the code. Also i dont really get the * and & and what exactly they do in the case. All i know is, that &variable stores the address from the RAM of the variable and that * creates a pointer (what ever that means?) or can somehow read what value at a &variable is...i am confused...here is the code:

    Code:
    #include <stdio.h>
    void f(int *);
    void f(int *ptr)
    {
      int ar[] = { 1, 2, 3, 4 };
      *ptr = &ar;
    }
    
    int main()
    {
      int *arr;
      f(&arr);
      for (int i = 0; i < 4; i++) {
        int d = arr[i];
        printf("The %d-th element is %d.\n", i, d);
      }
      return 0;                     //return Code 0 for all good
    
    }
    Last edited by Salem; 11-04-2022 at 01:21 PM. Reason: Removed crayola

  2. #2
    Registered User
    Join Date
    Nov 2022
    Posts
    4
    btw those are the 2 warnings in line 6 warning: assignment makes integer from pointer without a cast [-Wint-conversion] and 12 warning: passing argument 1 of 'f' from incompatible pointer type [-Wincompatible-pointer-types]

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,439
    Compare with this.
    Code:
    #include <stdio.h>
    void f(int **);
    void f(int **ptr)
    {
      static int ar[] = { 1, 2, 3, 4 };
      *ptr = ar;
    }
     
    int main()
    {
      int *arr;
      f(&arr);
      for (int i = 0; i < 4; i++) {
        int d = arr[i];
        printf("The %d-th element is %d.\n", i, d);
      }
      return 0;                     //return Code 0 for all good
     
    }
    
    $ gcc bar.c
    $ ./a.out 
    The 0-th element is 1.
    The 1-th element is 2.
    The 2-th element is 3.
    The 3-th element is 4.
    If you have int a, the type of a is int.
    Saying &a gets you a type of int*, like for example int *pa = &a
    Saying *pa takes you back to where you started, ie an int, like for example int b = *pa;

    But if your variable is already a pointer like int *arr, taking the address of it yields a type of int**
    This is what the parameter type of your function should expect.

    Imagine the line of types
    int** int* int
    & moves you to the left, * moves you to the right.

    Finally for completely different reasons not related to pointers, the array in the function needs to be static.
    Returning a pointer to a local variable (which is about to go out of scope) is a bad thing.
    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
    May 2012
    Location
    Arizona, USA
    Posts
    904
    Quote Originally Posted by Salem View Post
    Imagine the line of types
    int** int* int
    & moves you to the left, * moves you to the right.
    I like this simple explanation of pointers! Do you mind if I steal it?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,439
    Go for it christop
    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
    Nov 2022
    Posts
    4
    Ok, so if I understood correctly, in the warning programm, we create a pointer called arr, by saying int*arr; then we give the actuall arr value(which isnt initialized yet) to the function f, since the argument &arr moves us from *arr to arr? Or is it the address of arr since the deklaration of int* arr didnt store it as a pointer, but just told the compiler to see the arr variable as a pointer/address variable? function f expects a pointer variable (is a pointer then same thing as an address in the memory?), so i guess what we gave was the adress of arr, which is now cast to *ptr. *ptr is set to the array we want to have in it, so *ptr must be the pointer to *arr. After changing that, if we now look up arr, itll show the contents of ar. The rest is trivial.

    Now with your code.
    In the main, nothing seems to have changed, so skipping to f. Which now expects a pointer to a pointer. So if i see this correctly, f(&arr) gave him the address of the pointer to arr, which is the same as a pointer to a pointer to arr? Then you set only the pointer from the pointer pointer variable to the actual ar. So i guess it is like saying the actual value of the pointer pointer is ar. But then, in the main, you can still access arr with the [index], not needing to address its actual pointer? Why is that?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,439
    > in the warning programm, we create a pointer called arr, by saying int*arr;
    Yes.

    > then we give the actuall arr value(which isnt initialized yet) to the function f, since the argument &arr moves us from *arr to arr?
    No, remember & takes you further away from the end type, so &arr is of type int**

    > Or is it the address of arr since the deklaration of int* arr didnt store it as a pointer, but just told the compiler to see the arr variable as a pointer/address variable?
    Yes, f expects the address of arr, and yes arr is a pointer to some address.

    > function f expects a pointer variable (is a pointer then same thing as an address in the memory?)
    Yes.

    > so i guess what we gave was the adress of arr, which is now cast to *ptr. *ptr is set to the array we want to have in it, so *ptr must be the pointer to *arr.
    If you change 'cast' to 'dereference', yes.

    > So if i see this correctly, f(&arr) gave him the address of the pointer to arr, which is the same as a pointer to a pointer to arr?
    Yes, but the "which...." should read as "which has the type pointer to pointer to int".

    > Then you set only the pointer from the pointer pointer variable to the actual ar. So i guess it is like saying the actual value of the pointer pointer is ar
    ptr is dereferenced, to update the value of arr in main, with the start of the array called ar.

    > But then, in the main, you can still access arr with the [index], not needing to address its actual pointer? Why is that?
    Because saying arr[i] is the equivalent of saying *(arr + i)

    Question 6.3
    then
    Arrays and Pointers

    You can hide one of the stars using a typedef.
    Code:
    #include <stdio.h>
    
    typedef int*    T;
    
    void f(T *);
    void f(T *ptr)
    {
      static int ar[] = { 1, 2, 3, 4 };
      *ptr = ar;
    }
     
    int main()
    {
      T arr;
      f(&arr);
      for (int i = 0; i < 4; i++) {
        int d = arr[i];
        printf("The %d-th element is %d.\n", i, d);
      }
      return 0;
    }
    Then compare with say just an int.
    Code:
    #include <stdio.h>
    
    typedef int    T;
    
    void f(T *);
    void f(T *ptr)
    {
      *ptr = 42;
    }
     
    int main()
    {
      T val;
      f(&val);
      printf("The answer is %d\n",val);
      return 0;
    }
    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
    Nov 2022
    Posts
    4
    alright, i am beginning to understand this, Thank you so much. We havent had typedef yet --> arent allowed to use stuff we didnt cover, so ill come back on that later. I see & makes a * variable a ** variable. Ill go over this a few more times to really internalize whats happening

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-23-2013, 01:44 PM
  2. Get rid of compiling warning?
    By tmac619619 in forum C Programming
    Replies: 7
    Last Post: 10-13-2012, 03:37 PM
  3. Compiling Warning
    By Micki-Zee in forum C Programming
    Replies: 3
    Last Post: 06-29-2011, 09:40 AM
  4. Replies: 3
    Last Post: 03-01-2011, 07:00 PM
  5. Compiling warning..
    By Jez_Master in forum C++ Programming
    Replies: 2
    Last Post: 05-01-2002, 10:45 PM

Tags for this Thread