Thread: pointer question

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    96

    pointer question

    i have a question regarding to pointer. i hope someone can explain it to me with basic english please .

    Code:
    for example  1;
     char name[15]={'j','o','h','n'};
    char *ptr;
    
    ptr =name; //why dont i need and & sign
    
    for example  ;
     int x=5;
    int *t;
    t=&x;     // why & here but not in array    
    ptr =name; why dont i need and & sign
    Also can i point to something that has different types for example char point to int.

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    96

    pointer question

    i have a question regarding to pointer. i hope someone can explain it to me with basic english please .

    Code:
    for example  1;
     char name[15]={'j','o','h','n'};
    char *ptr;
    
    ptr = &name; // is this the same as ptr =name
    ptr =name; //why dont i need and & sign
    
    for example  ;
     int x=5;
    int *t;
    t=&x;     // why & here but not in array
    Also can i point to something that has different types for example char point to int.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In C, the name of the array, serves as a constant pointer to the base of the array (that's the array[0]). You can't change the address of the array name!

    Why wouldn't this work:
    Code:
    int *t;
    int n;
    
    t=&n;
    t is a pointer, and what do pointers hold as their value? Addresses! And what is &n? The address of n.

    Basically, NO, a char pointer shouldn't be used to point to an int. It can be done, but if you want to point to different data types, a void pointer or a pointer that matches that type, is the way to go. A char pointer, for instance, is looking for only 8 bytes, (maybe 16). So it would only "see" 1/2 or maybe 1/4th, of an integer, at that location. (depending on the sizes of char and integer, on your system).

    You sound like you want to work with pointers, unions, and casts! They are interesting, for sure.

    Good to go?
    Last edited by Adak; 10-25-2010 at 01:40 AM.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by mouse666666 View Post
    Code:
    for example  1;
     char name[15]={'j','o','h','n'};
    char *ptr;
    
    ptr = &name; // is this the same as ptr =name
    ptr =name; //why dont i need and & sign
    
    for example  ;
     int x=5;
    int *t;
    t=&x;     // why & here but not in array
    You don't need an & sign for "ptr = name" because name is the location of the array (i.e. "name" is where the array is). In this case, "ptr= &name" is just there for ease of use. To get the location of x you need the &, but since the array name is already a location it is not needed.


    Also can i point to something that has different types for example char point to int.
    Only if you use a union.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    This is actually an invalid assignment

    Code:
    ptr = &name; // is this the same as ptr =name
    You are assigning pointer-to-Pointer to pointer. Name is already a pointer and assigning a address of a pointer to a variable which is just a pointer. The '*ptr' should have been a pointer-to-pointer to make above assignment. For example

    Code:
    char **ptr;   // pointer-to-pointer to hold the address of pointer
    char *ptr1;   // pointer to hold an address
    char name[] = "Test string";
    
    ptr = &name;   // assigning address of pointer 'name'. Where 'name' itself is pointing to an array
    ptr = name;
    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    And I also din't realise this was duplicate post

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Threads merged.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Easy pointer question
    By Edo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2009, 10:54 AM
  3. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 11:59 AM
  4. Pointer question
    By rakan in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 02:23 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM