Pointers in C

This is a discussion on Pointers in C within the C Programming forums, part of the General Programming Boards category; Can someone explain pointers, I am having trouble understanding them. 1. If I declare a pointer called 'pointer' how can ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Location
    Texas
    Posts
    5

    Question Pointers in C

    Can someone explain pointers, I am having trouble understanding them.

    1. If I declare a pointer called 'pointer' how can I point it to an object float type ?

    2. Can a pointer be initialized to zero, or a address or any value ?

    3. What kind of operator returns the location in memory where a variable is stored, and what operator returns a value of where a pointer variable is pointing to?

    thanks,

    - N

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ok do look on the boards for the "what is a pointer" question as I know i've seen it at least 50 other times. And a float pointer would be:

    Example:
    Code:
    float *pointer;
    And to get a memory address you would do this:

    Example:
    Code:
    float a = 1.55;
    float *pointer = &a;

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Wow, this looks a lot like homework.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >1. If I declare a pointer called 'pointer' how can I point it to an object float type ?
    Your book, or any tutorial, can tell you this immediately.

    >2. Can a pointer be initialized to zero, or a address or any value ?
    Yes for the first two, but pointing directly to an arbitrary address is not a portable feature. A pointer can only point to an address, and values aren't addressable quantities.

    >3.
    This one reeks of homework, and any book or tutorial will give you the answer.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Location
    Texas
    Posts
    5
    master5001,

    Thanks for the speedy response,

    Ok do look on the boards for the "what is a pointer"...
    Ill look into it.

    Wow, this looks a lot like homework
    kind of its review for exam

    ----------------------------------

    I also have another question about working with larger programs.

    Lets say I have four diffrent source pro1.c, pro2.c pro3.c pro4.c.
    Where pro1.c contains the function main.

    All of them contain a
    Code:
    printf
    Where do I put
    Code:
    #include <stdio.h>
    Why is it beneficial to split the program?

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Where do I put #include <stdio.h>
    In every file that you use printf. Otherwise bad things can happen.

    >Why is it beneficial to split the program?
    You won't see the benefit until you've worked on a program with several thousand lines of code. Splitting files is an organizational trick to make your life easier.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Dec 2004
    Location
    Texas
    Posts
    5
    okay thanks Prelude

    One more question then Ill stop bugging you. Back to my pointer question can a pointer be initialized to a Symbolic Constant NULL?

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Why is it beneficial to split the program?
    One reason is for compilation speed. Once you get part of your program working correctly, you compile it into an object file. Then, as you work on other files you can compile them and link them with the object file. Instead of having to recompile the entire program again you only have to recompile the files you're working on.
    If you understand what you're doing, you're not learning anything.

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Quote Originally Posted by Nancy7904
    can a pointer be initialized to a Symbolic Constant NULL?
    You should be able to test this one yourself with a 4-line program.
    If you understand what you're doing, you're not learning anything.

  10. #10
    Widdle Coding Peon Aerie's Avatar
    Join Date
    Dec 2004
    Posts
    115
    Okay, here's the explanation I was given.

    To understand variables and pointers in C, you really have to understand that variables in C aren't quite like variables in other languages.

    Basically, most languages treat a variable as a box you put things in. You declare the variable, then you can store things in or retrieve things from it.

    C treats variables a tiny bit differently. In reality, ALL variables are pointers, but "normal" variables are treated like permanently dereferenced pointers; that is, when you say somevar = 5; you're saying "the block of memory at the address pointed to by somevar is equal to 5."

    When you declare a pointer, you're basically making a pointer that doesn't lie about being a pointer, so it 'naturally' acts like a pointer, so saying ptr = &somevar; is basically saying 'the address ptr points to is now the same as the address pointed to by somevar.' Now, if you dereference ptr, you are accessing the section of memory allocated to somevar. In other words, if you do *ptr += 2; you're setting whatever section of memory ptr points to, in this case the memory allocated to somevar, to itself plus 2, in this case setting itself to 7.

    When you print out the value of somevar, after doing this, the output will be 7, even though you modified the value of somevar through ptr.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    int *ptr;         /*Declaring a pointer*/
    int notaptr;      /*Declaring a normal variable*/
    
    notaptr = 5;
    ptr = &notaptr;        /*Assigns the address of notaptr, instead of its value, because notaptr*/
                           /*is being made to act like a pointer because of the & in front of it.*/
    
    *ptr += 3;             /*The * in front of ptr makes it act like a variable, in this case*/
                           /*ptr is 'being' notavar, since that's the address it was assigned to.*/
    
    printf("%d", notaptr); /*Prints 8, since the value of notaptr was modified using ptr*/
    return 0;           
    }
    Pointers can be used in a lot of ways, especially to allocate memory(often using arrays), or to allow a function to modify a variable local to another function(remember local variables which aren't static go away after their host function terminates!).

    I may be wrong on some of these details, so if I am, someone please correct me.

    Edited to complete the post, hit the save button instead of preview.

    Edit: One notable use of pointers is with arrays. What an array is is basically a pointer that points to a number of variables of the array's type which reside in a contiguous section of memory.

    In other words, you declare an array int somearray[10]; and you've basically got 10 int-sized sections of memory all in a row. When you assign a pointer to somearray's address, it's assigned to somearray[0]. When you assign a value to somearray[4], you can later access it by assigning the pointer to somearray, then incrimenting that pointer 4 times, which makes it move 'up' 4 times from somearray[0] to somearray[4]. If you were, after this, dereference that pointer, it would contain the value of somearray[4]. You could then incriment or decriment the pointer to point to other elements of the array as needed.

    Again, I may be wrong in some(or all) of the details here, but I think that's basically how it works. I'm hoping as a fellow C-programmer-in-the-making, I can give you a lateral perspective on the subject, which might be lost to those who've come to accept this stuff as second nature.
    Last edited by Aerie; 12-16-2004 at 04:07 PM.
    I live in a giant bucket.

  11. #11
    Registered User
    Join Date
    Dec 2004
    Location
    Texas
    Posts
    5
    itsme86, thanks for your help

    from the book-
    There is a special pointer called NULL. It points to nothing. (The actual numeric value is 0.) The standard include file, locale.h, defines the constant NULL. (This file is usually not directly included, but is usually brought in by the include files stdio.h or stdlib.h.)


    Aerie

    thanks for that lenghty explanation of pointers I am still a bit confused...lets say I have the following code

    Code:
    int values[5] = {20, 45, 40, 33, 60},  *Ptr;
    
    Ptr = values;
    Ptr++;
    
    //I am at 45 right? 
    
    //but what address is refrenced if?
    
    Ptr + 3;
    
    //33?
    yes typo sorry
    Last edited by Nancy7904; 12-16-2004 at 04:44 PM.

  12. #12
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    I'm not sure if vPtr is a typo and you meant to do Ptr, but anyway...Ptr + 3 is equivalent to &Ptr[3]. So Ptr +3 would be a pointer to the memory containing the value 33 (if you don't count the first incrementation).
    If you understand what you're doing, you're not learning anything.

  13. #13
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Ptr + 3 is equivalent to &Ptr[3]
    Actually, Ptr+3 is equivalent to just Ptr[3].

    EDIT: NM, I interpreted what you said incorrectly.

  14. #14
    Registered User
    Join Date
    Dec 2004
    Location
    Texas
    Posts
    5
    if you don't count the first incrementation
    What do you mean? What would I end up with if I count the first incrementation?
    Code:
    Ptr += 3;
    I get 33
    Code:
    Ptr + 3;
    I get 20 is this what you get too?

  15. #15
    ---
    Join Date
    May 2004
    Posts
    1,379
    Code:
    int i, *ptr;  // an int and a pointer to an int
    
    i = 0; // int i is equal to 0
    ptr = &i  // ptr is equal to the address of i
    
    // When you want to change the value of what the pointer is pointing at then use the * operater
    *ptr++;
    
    printf("%d",i); // Output is 1

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21