# Thread: Pointers

1. ## Pointers

its been around 1 week i started C.im learning it from "teach yourself C in 21 days".im in the 9th day and ive finished the chapter and doing the exercises.theres an exercise that asks "how to assign the address of a float value called radius to a pointer"
at the back of the book the answer of this exercise is just

Code:
`float *var = &radius;`
what i tried on the VC++ 6 compilter is

Code:
```#include<stdio.h>
float *var,radius=4;

main()
{
*var=&radius;
printf("var has the address of &#37;d and it points to %f",var,*var);
return 0;
}```
unfortunately im getting the error " 1.c(6) : error C2115: '=' : incompatible types "
I tried var=&radius instead of *var=&radius and it worked.

but for the case of passing arrays as arguments to functions:

Code:
```#include<stdio.h>
int array[10];
void f1(int);

main()
{
f1(array);
return 0;
}

void f1(int *x)
{printf("%d",x);}```
After compiling im getting warnings.
1.c(7) : warning C4047: 'function' : 'int ' differs in levels of indir
m 'int [10]'
1.c(7) : warning C4024: 'f1' : different types for formal and actual p
1.c(12) : warning C4028: formal parameter 1 different from declaration

but the * in the void f1(int) is fixing the warnings

can someone explain why doesn't the *var=&radius work in the first code but when passing array to the *x in function f1 (as void f1(int *x)) its working ? like they are the same thing ... in the second code we are trying to *x = array , by passing array to the argument ( int *x)
also why is the Warnings for and how come they are fixed when in the function prototype the f1(int) is replaced by f1(int *) ?

2. I tried var=&radius instead of var=&radius and it worked.
var is a pointer, *var is what it points to.

3. First of all, main should return int.
Second, I can suggest you actually update your compiler. VC6 is very old and outdated.
And as for your problems, function prototypes and definitions must match each other. The compiler will look at the declaration (prototype) of the function and sees "int", yet you try to pass int*, so it complains.
And I suggest perhaps you re-read the pointers part a little, because doing *var will actually dereference the pointer and not assign something to the pointer itself, but to the memory it points to. Thus you try to assign int* to int, which also won't work.

Code:
`{printf("&#37;d",x);}`
I wouldn't recommend doing that either. Firstly, separate the lines:
Code:
```{
printf("%d",x);
}```
Second, don't print pointers using %d. There's a special type for that. Don't remember what it is, though.

4. Second, don't print pointers using &#37;d. There's a special type for that. Don't remember what it is, though.
cppreference.com says it is %p, but I cannot be bothered to double check with C99.

5. Originally Posted by Elysia
Second, don't print pointers using %d. There's a special type for that. Don't remember what it is, though.
it is %p - easy for pointer

6. Originally Posted by laserlight
var is a pointer, *var is what it points to.
yeah i know, i knew that its done by
var = &radius.

but the answer in the book confused me.
plus it is working when trying to pass an address as an argument when the function's argument is *x. its like *x = &array[0].
?

7. Well, the book is wrong.
I still don't fully understand your first question, though?
And remember that all pointers must have a type. *x is not a pointer, it is dereferencing the pointer named x.
A real pointer is type* x (or type * x or type *x, whichever you want).

8. Quote:
Originally Posted by Elysia View Post
First of all, main should return int.
Second, don't print pointers using %d. There's a special type for that. Don't remember what it is, though.
im just in the 9th chapter.havent finished the whole book yet.all i know are the %d,%f,%c and some which i still didnt use.
and throughout the chapter all the pointers were printed using %d.

for the return, its still =0 throughout the 9 chapters

Quote:
Originally Posted by Elysia View Post
First of all, main should return int.
Second, I can suggest you actually update your compiler. VC6 is very old and outdated.
.
what do u advise me to dl ?

9. Here's a list of free compilers:
http://cpwiki.sf.net/Integrated_Development_Environment

If you're a student and live in any of the following countrries:
United States, the United Kingdom, Canada, China, Germany, France, Finland, Spain, Sweden, Switzerland and Belgium
You can also get the Professional version of Visual Studio free at https://downloads.channel8.msdn.com/

10. thank you pal.im admiring your superfast replies

11. but still the concept of how the *x = &array[0] is true while passing array as an argument to the void f1(int *x) is not clear .

12. Why not show code to demonstrate what you mean?

13. Originally Posted by Elysia
Why not show code to demonstrate what you mean?
check my 1st post

14. All I see is this
Code:
```int main()
{
f1(array);
return 0;
}

void f1(int* x)
{
printf("&#37;d",x);
}```
Are you wonder why this is working or something else?

15. the codes in my 1st post are

Code:
```#include<stdio.h>
float *var,radius=4;

main()
{
*var=&radius;
printf("var has the address of %d and it points to %f",var,*var);
return 0;
}```
Where the *var = &radius is not working

the 2nd code is

Code:
```#include<stdio.h>
int array[10];
void f1(int);

main()
{
f1(array);
return 0;
}

void f1(int *x)
{
printf("%d",x);
}```
where it seems to work ...

Popular pages Recent additions