Thread: Simple Pointer Question

  1. #1
    Registered User
    Join Date
    Jan 2004

    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:

    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:

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


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

    You can do this with an array
    char arr[10];
    char *p = arr;
    char *q = &arr[0];
    So you can do this
    char *p = "string";
    char *q = &"string"[0];
    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
    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