0

I am trying basic c program using pointers, Here's my code

#include <stdio.h>

int main()
{
    int *p;
    
    for(int i=0;i<10;i++){
        *p = &i;
        printf("%d",*p);
    }
    
    return 0;
}

Output:

main.c:16:12: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
         *p = &i;
            ^

...Program finished with exit code 0

And the value is not printed. Is there any problem in assigning variable i which persists from for loop to pointer?

Ayush Naik
  • 111
  • 2
  • 14
  • This is more "C" then "C++". (include and printf).. But you are trying to assign an address (&i) to an int (*p), *p means thing p points to. So you probably would meant "p=&i". – Pepijn Kramer Sep 12 '21 at 06:12
  • `int *p` declares the pointer `p`. When assigning the address to `p`, you would do `p = &i` and to output the address you use the `%p` conversion specifier, e.g. `printf("%p\n",(void*)p);` If you did want to print the value at that address, then you would use `printf ("%d\n", *p);` to print the value of `i`. – David C. Rankin Sep 12 '21 at 06:13
  • A few links that provide basic discussions of pointers may help. [Difference between char *pp and (char*) p?](https://stackoverflow.com/a/60519053/3422102) and [Pointer to pointer of structs indexing out of bounds(?)...](https://stackoverflow.com/a/60639540/3422102) (ignore the titles, the answers discuss pointer basics) – David C. Rankin Sep 12 '21 at 06:14

4 Answers4

0

P is the pointer. Therefore, assign the pointer to it as follows.

p = &i;

Then use it as follows in your code.

#include <stdio.h>

int main()
{
    int *p;
    
    for(int i=0;i<10;i++){
        p = &i;
        printf("%d",*p);
    }
    
    return 0;
}
Kavindu Vindika
  • 2,449
  • 1
  • 13
  • 20
0

Use this code. It'll work, I think

int* p =NULL;
*p = sizeof(i); // casting int to pointer
schlebe
  • 3,387
  • 5
  • 37
  • 50
zain ul din
  • 1
  • 1
  • 2
  • 23
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 12 '21 at 07:42
0

With fix and more C++ style using std::cout for output. (Why not use printf() in C++)

#include <iostream>

int main()
{
    int* p{ nullptr }; // initialize before use. 

    for (int i = 0; i < 10; i++) 
    {
        p = &i;
        std::cout << *p << " ";
    }

    return 0;
}
Pepijn Kramer
  • 9,356
  • 2
  • 8
  • 19
0

Here P is an integer Pointer, And pointer is a variable which store the address of the another variable. So you have to assign the the address of variable (i) to variable (p), like

p=&i;

In this line you are storing the address of i in variable p. When you point p like, *p it will give you the value present at that address.

#include <stdio.h>

int main()
{
    int *p;
    
    for(int i=0;i<10;i++){
        p = &i;
        printf("%d",*p);
    }
    
    return 0;
}
HI2000
  • 1
  • 1