Thread: Im so lost at . .

  1. #1
    Registered User hermit's Avatar
    Join Date
    Apr 2002
    Posts
    213

    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
    Last edited by hermit; 05-14-2002 at 08:35 AM.

  2. #2
    Unregistered
    Guest
    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.
    Ask more questions.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    // 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.

  4. #4
    Registered User hermit's Avatar
    Join Date
    Apr 2002
    Posts
    213
    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

  5. #5
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    how does the compiler knows that *p is pointing to an array
    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
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User hermit's Avatar
    Join Date
    Apr 2002
    Posts
    213
    1) void display_marks( float *, int);
    is this right? telling the compiler that it is pointing to a float ?
    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
    Last edited by hermit; 05-11-2002 at 07:26 AM.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    That looks OK to me.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User hermit's Avatar
    Join Date
    Apr 2002
    Posts
    213

    Thumbs up

    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 . .
    Last edited by hermit; 05-11-2002 at 10:16 AM.

  10. #10
    Registered User hermit's Avatar
    Join Date
    Apr 2002
    Posts
    213
    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

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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
    >click on your head? tell me tell me
    I just did.

    -Prelude
    My best code is written with the delete key.

  12. #12
    My diaper's full....... stevey's Avatar
    Join Date
    Nov 2001
    Posts
    746
    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 !!!!
    Last edited by stevey; 05-14-2002 at 01:25 PM.
    Steve

  13. #13
    Registered User hermit's Avatar
    Join Date
    Apr 2002
    Posts
    213

    Thumbs up

    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??

  14. #14
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    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
    Last edited by sean345; 05-14-2002 at 10:04 AM.
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  15. #15
    Registered User hermit's Avatar
    Join Date
    Apr 2002
    Posts
    213
    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);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The getting lost and walking in circles
    By Mario F. in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 05-07-2008, 09:53 AM
  2. I lost my laptop, DVD and money
    By Sang-drax in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 10-01-2004, 07:13 PM
  3. lost disk
    By Benzakhar in forum Linux Programming
    Replies: 7
    Last Post: 01-11-2004, 06:18 PM
  4. Lost ID number
    By ripper079 in forum C++ Programming
    Replies: 13
    Last Post: 10-04-2002, 12:51 PM
  5. API, LOST... help
    By Unregistered in forum Windows Programming
    Replies: 5
    Last Post: 03-13-2002, 03:19 PM