Thread: Simple Pointer Question

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    3

    Simple Pointer Question

    Let me start by saying I am fairy new to C programming, but not programming in general.

    Say you have the code below which defines a pointer to an array of chars (a string right? heh), and then you want to assign some text to that string. From the tutorials I have read, and the coding I have tried I know this works:

    Code:
    char *name;
    name = "Reason58";
    Now given that pointers are variables which store the memory address of other variables, why is the above code acceptable? Shouldn't it be:

    Code:
    char *name;
    name = &"Reason58";
    Or something similar? Sorry for the stupid question. I have pointers like 98% figured out, just wanted to clarify that.

    Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    A "string" has the same type as a char array

    You can do this with an array
    Code:
    char arr[10];
    char *p = arr;
    char *q = &arr[0];
    So you can do this
    Code:
    char *p = "string";
    char *q = &"string"[0];
    http://pw2.netcom.com/~tjensen/ptr/cpoint.htm
    Is a really good explanation of pointers
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2004
    Posts
    3
    Great. Thank you for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple pointer question
    By Sharke in forum C Programming
    Replies: 2
    Last Post: 04-16-2009, 01:05 AM
  2. a pointer to a function question..
    By transgalactic2 in forum C Programming
    Replies: 17
    Last Post: 10-21-2008, 11:47 AM
  3. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM
  4. Pointer and segfaults question
    By kzar in forum C Programming
    Replies: 5
    Last Post: 09-15-2005, 09:03 AM
  5. pointer to pointer as argument question
    By Lateralus in forum C Programming
    Replies: 4
    Last Post: 07-21-2005, 05:03 PM