Pointers

This is a discussion on Pointers within the C Programming forums, part of the General Programming Boards category; PLEASE CAN SOMONE EXPLAIN POINTS TO ME IN SIMPLE TERSM!! WHAT ARE THEY? CAN YOU RELATE IT TO AN EVERYDAY ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    1

    Exclamation Pointers

    PLEASE CAN SOMONE EXPLAIN POINTS TO ME IN SIMPLE TERSM!! WHAT ARE THEY? CAN YOU RELATE IT TO AN EVERYDAY SCENIRIO? I CANNOT GET MY HEAD AROUND THEM

  2. #2
    Nit
    Nit is offline
    Registered User
    Join Date
    Mar 2002
    Posts
    42
    A pointer is just a variable that holds a memory address.

    [Edit]I'm not a very good teacher, so my analogy might just be that terrible...

    int *y;
    y is now a pointer to an integer. The value of y is the memory address that it references (which right now is NULL). The value of *y is the value of the memory address that it references.

    example:

    int k = 7;
    int *y;

    y = &k;
    y equals the address of k. y now points to the memory address of k. *y is equal to 7.

    Just think of a variable declared as a pointer as a box. The box should always have a line from it to memory (to what it's referencing). If you give the box a new value think of yourself drawing a new line from the box to somewhere else in memory.

    I hope this terrible explanation helped you a little bit.


    Do you also need help with pointer arithmetic?
    Last edited by Nit; 03-26-2002 at 12:02 PM.
    http://www.KBeutler.com

  3. #3
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    THERE ARE MANY EXPLANATIONS ON THIS SITE ALREADY. USE THE SEARCH BUTTON. BY THE WAY, YOUR CAPS LOCK BUTTON IS STUCK!

  4. #4
    Unregistered
    Guest
    A pointer is just an address. If I gave someone a letter for Marcus, they may not know who Marcus is and would require the address for Marcus. Variable names are generally lost when you exit a function, so you use addresses, which don't change. You've probably noticed that often something will have a variable name in main which is different from its name in a function. The address is what links them together.

  5. #5
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,420
    I know a pointer is a variable that holds an address... what i don't understand is why this works:

    char *a = "String";

    Is it the same as

    char arr[] = "string";
    char a* = arr;

    ?

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Now a and arr are pointing to the same string. (char *a instead of char a*)

  7. #7
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,420
    right, i get that.

    I understand:

    char arr[] = "string";
    char *a = arr;

    you assign "string" to an array arr, and the system counts the string for you effectively putting "string" into arr[6].

    What i'm not so sure about is what is going on here (this being a seperate example from the above):

    char *a = "string";

    how can a pointer point at a string that hasn't been put into an array?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > char arr[] = "string";
    > char *a = arr;
    >
    > you assign "string" to an array arr, and the system counts the
    > string for you effectively putting "string" into arr[6].


    Slight correction here. The assignment of "string" to 'arr[]' puts
    those letters in the first six characters, of 'arra', and adds the
    NULL in 'arr[6]'. I believe this is what you meant to say.

    > char *a = "string";
    >
    > how can a pointer point at a string that hasn't been put into an
    > array?

    This is called a "string literal". A strin literal is a block of memory
    set aside to house a string declared in this manner. You (if I
    recall correctly) cannot free( ) string literals. They are allocated in
    the string table, and are permanantly afixed there for the life of
    the application.

    Furthermore, if you do this:

    char *a = "string";
    char b[] = "string";

    a = b;

    Then the assignment causes you to loose your string literal. It
    still exists some place, but you no longer have access to it due
    to the fact that nothing points to it any more.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Originally posted by Clyde
    how can a pointer point at a string that hasn't been put into an array?
    Either it's on a heap or a stack.......one of the two

  10. #10
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,420
    Thanks for the explanation quzah.

    Is there any reason why you would want to place a string in a string literal rather than an array?

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >why you would want to place a string in a string literal rather than an array?
    If you don't want to modify the string, declare it as a string literal. That way if you accidentally try to modify it, you'll get an error.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 10:23 AM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 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