# Im so lost at . .

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 05-11-2002
hermit
Need help with pointers
POINTERS

Here is the code:

#include <stdio.h>
#define SIZE 7

void display_marks( float *, int);

int main()
{
float marks[SIZE] = {10, 11, 12, 13, 14, 15, 16};
display_marks(marks, SIZE);
}

void display_marks(float *p, int size)
{
int i;
for(i=0; i<SIZE; i++)
printf("%.2f\n", p[i]);
}
This is a working program, but i do not understand a few tasks..

a couple of questions i need to ask:
1. at void display_marks( float*, int)
what does float* do? how come it is intialise at float*, pointing to what?what does it mean?call to function , isnt it passing the value of float marks[SIZE] instead?

2. void (float *p, int size)
again, how come it is float*p now? pointing to what?
an last but not least how come, p[i] prints the value of the array

peat and repeat IM LOST, if you could help please explain in simple english. Thanks guys
• 05-11-2002
Unregistered
Marks is a float array and *float is a pointer to it.
compare the function call with the function:
display_marks(marks, SIZE);
void display_marks(float *p, int size)
You can't pass an array, so you pass a pointer to the first element plus an integer for the size of the array.
• 05-11-2002
MrWizard
// 1. at void display_marks( float*, int)

This is the function prototype. Because your when code begins to get executed in 'main' and you make a call to "display_marks" at that point it does not know what that means. You write in a function prototype (what that is void display_marks(float *, int )) to tell the compiler, look i'm expecting this function call and I'll define it later in this file. You don't need a function prototype if you declare the function AND the body BEFORE the main function. Generally the prototype is the way to go.

// 2. void (float *p, int size)
// again, how come it is float*p now?

Well, before on the prototype the parameters were just float* and int. This says I expect a pointer to a float as well as an integer. What you are seeing now is the actual variable names that the infomation will be stored in for the local function. This is where you say p will be the pointer to a float that is passed in and size will be my variable for the int passed in.

Hope this clears up a little?? If you have any questions, message me. Hope I didn't confuse you further.
• 05-11-2002
hermit
how does the compiler knows that *p is pointing to an array?
it is not declared anywhere . .

i got it but not really . . sorry guys
• 05-11-2002
C_Coder
Quote:

how does the compiler knows that *p is pointing to an array
Quote:

void display_marks( float *p, int size);

float marks[SIZE] = {10, 11, 12, 13, 14, 15, 16};
display_marks(marks, SIZE);
The compiler dosent need to know that p is pointing to an array, all it needs to know is that p is pointing to an address of the correct type. wether the function accesses the array correctly or not is up to you.
An array name is itself a pointer to the first element of that array so by calling the function with the array name you are passing the address of the first element.
Consider how the scanf function works, it works the same way and you probably used it a thousand times:
When you read a character with scanf you have to use the '&' to tell scanf the address to write the character to, when you want to read a string into an array you dont need the '&' because the array name is a memory address.
Code:

```char ch; char buffer[10]; scanf( "%c", &ch );        // gets a character scanf( "%s", buffer );    // gets a string```
• 05-11-2002
Hammer
Quote:

Originally posted by hermit
how does the compiler knows that *p is pointing to an array?

It doesn't, but it doesn't matter in this case.

>ptr[i]
just means add i to ptr, in the same manner as any other pointer arithmatic works.

In your code, p is first set to the start address of the input array, then by adding i to the pointer, you'll find each element.
• 05-11-2002
hermit
Quote:

1) void display_marks( float *, int);
is this right? telling the compiler that it is pointing to a float ?
Quote:

2)display_marks(marks, SIZE);
display_marks passes marks which is a content of array into display mark function; that is float *, and int SIZE

am i making sense? is this right? i know im confuse, hope i didnt confuse you guys
• 05-11-2002
Hammer
That looks OK to me.
• 05-11-2002
hermit
im still having problems with pointers. im currently learning how to write a struct to a file using pointers.

i need to get some sleep, i'll post for more help tomorrow . .
• 05-14-2002
hermit
okay im back!

im trying to understand pointers. well it is hard. what would you recommend? me to do or read? in simple everyday english. not in big comp jargons.

im trying to understand . ...

the difference between passing a structure to a function by reference and value.

i know , if i cant do pointers i can forget about doing C, the most important part of c language i suppose. .

how did pointer clicked for you? think back to those days that u went thru pointers. . how did it click on your head? tell me tell me
• 05-14-2002
Prelude
>im trying to understand pointers. well it is hard.
Not it isn't, you are making it so. A pointer is a variable, just like int and char or double. But instead of containing a user or computational defined value, a pointer contains an address. An address is usually just a hexadecimal value which defines a location in memory. When you remember that a pointer is only a variable that holds an address, it gets much simpler.

>the difference between passing a structure to a function by reference and value
C doesn't have passing by reference, but you can simulate it by using pointers. Remember that a pointer holds an address? If you have a pointer hold an address to your structure, you can then pass the pointer by value to a function. A copy is made of the pointer but it still contains the address of the structure and you can modify the original structure. Without pointers you would have to pass the structure and a copy of it would be made, you couldn't modify the original from inside the function.

>think back to those days that u went thru pointers. . how did it
I just did. :)

-Prelude
• 05-14-2002
stevey
explaining Preludes comments for us nuggets , consider ->

#include <stdio.h>

void swap(int from, int to)

int main()
{
int num1=12;
num2=5;
printf("value of num1=%d,value of num2=%d,num1,num2);
swap(num1, num2);
printf("value of num1 now =%d,value of num2 now=%d,num1,num2);

}

void swap(int from, int to)
{
int temp;
temp=from;
from=to;
to=temp;
}

program produces
value of num1=12, value of num2=5
value of num1 now=12, value of num2 now=5

ie values not swapped..because when a function is called in this way its using 'copies of the actual parameters' the function call is 'call by value' ie

whats happened is-
temp=from (- store value stored at 'from' in 'temp')
from=to (- store value stored at 'to' in 'from')
to = temp (- store value stored at 'temp' in 'to')

the addresses of from, temp and to are not the same as those used to store num1 and num2. they are new ones, so num1 and num2 are not affected by the swap.
.................................................. .......................
so to swap, you need

#include <stdio.h>

void swap(int *num1_ptr, int *num2_ptr);

int main()
{
int num1=12;
num2=5;
printf("value of num1=%d,value of num2=%d,num1,num2);
swap(&num1, &num2);
printf("value of num1 now =%d,value of num2 now=%d,num1,num2);

}

void swap(int *num1_ptr, int *num2_ptr)
{
int temp;
temp=*num1_ptr;
*num1_ptr=*num2_ptr;
*num2_ptr=temp;
}

program produces
value of num1=12, value of num2=5
value of num1 now=5, value of num2 now=12

ie swap has occurred.this is becos -

temp=*num1_ptr;
( store value 'at the address'pointed to by num1_ptr(12) in temp)

*num1_ptr=*num2_ptr;
(store value 'at the address' pointed to by num2_ptr(ie 5) 'at the address' pointed to by num1_ptr(ie 12). this statement overwrites the original value at the address(12) with the new value 5)

*num2_ptr=temp;

(store the value stored in temp 'at the address' pointed to by num2_ptr with the value stored in temp.(12) )

so the 2nd program has 'passed by reference' ie reference to the addresses of the arguments.

does this help at all ???

think of the scanf function, you pass the addresses of the indentifiers to the function...

also functions that relate to arrays...you can't pass the entire contents of an array to a function, only the address of the array.
the array name is considered as a constant pointer to the 1st element in the array, so functions involving arrays are always called 'by reference'. using a pointer you can then manipulate the contents of the array.....ie

void string_to_upper (char s[80])
{
int c;
for (c=0;s[c]!='\0';c++)
s[c]=toupper(s[c]); edit [whoops thats not using pointer!!]
}

anyway, if i can get it anyone can.i'm a mental midget!!, keep at it....pointers are possibly the hardest part of C IMO, so when you've got it you're away !!!!
• 05-14-2002
hermit
while trying to conceptualise what prelude's and stevey's post

im gonna ask another silly question !

here it is

Q: what is the difference between & and *?

eg scanf("%d", &num);

isnt that pointing to the address of num?
how does & and * come hand in hand??
• 05-14-2002
sean345
When you are using scanf you need to pass the address of the variable you wish to get input for. So you can use the & sign.
Code:

`scanf("%d", &num);`
Any time you have a variable and want the address you can use the & sign.

If you have a pointer you can use the * sign to access the value. For example:
Code:

```int Var = 5; int *ptr = &Var; printf("%d\n", *ptr); *ptr = 1; printf("%d", Var);```
This creates a int variable named Var and sets it to 5. It then creates a pointer which points to the memory location of Var. When you print *ptr it is print the value of what it points to, in this case it is 5.

When you set the value of ptr to 1 you also change Var to 1. Thus, it prints 1 next.

If you just use ptr without the * you would be printing the address it points to, in this case it would be the same as &Var.
Code:

```int Var = 5; int *ptr = &Var; printf("%p\n%p", ptr, &Var);```
- Sean
• 05-14-2002
hermit
like this example:

#include <stdio.h>

void main(void)
{ float *ptr, data = 100;

ptr = &data;
printf("%p %p\n", ptr, &data);
printf("%f %f %f\n", data, *ptr, *&data);
}
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last