Thread: Declaring pointer to variable in struct

  1. #1
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72

    Declaring pointer to variable in struct

    Hello. If I have the following struct

    Code:
    struct rec {
           char name[20];
           int age;
    }p1;
    
    struct rec *ptr = &p1;
    
    strcpy(ptr->name, "John");
    ptr->age = 20;
    How do I declare a pointer to "name" within the structure so that I could
    use it to move through the name array for whatever reason? I know
    that "ptr->name" is a pointer to name, but can I increment this pointer to access
    individual bytes within name[]?

    Sorry, I'm kinda new to structures.
    thanks
    Remember that all that code you write turns into this:

    0100100100110010010011100100111001001
    0010100100100001001111100010010010010 ....

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    ptr->name[0]
    for the first char.

    or

    Code:
    char * chp = ptr->name;
    *(++chp) = 'a'; /* to set the second char to 'a' */
    Kurt
    Last edited by ZuK; 07-09-2007 at 11:42 AM. Reason: Was wrong

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Code:
    char *p = ptr->name;
    Walk.

    Safer way is probably to use ptr->name as the array that it is and index it with a counter variable.

    Edit: Slow again.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You can index the pointer, or use another pointer:
    Code:
    for ( i = 0; ptr->name[i] != '\0'; i++ )
      printf ( "%c\n", ptr->name[i] );
    
    for ( p = ptr->name; *p != '\0'; p++ )
      printf ( "%c\n", *p );
    name is an array, so you can't use it as a pointer to iterate over the contents of the array.
    My best code is written with the delete key.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Like
    char *cp = &p1.name;

    > Edit: Slow again.
    Call me Mr Glacier
    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.

  6. #6
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72
    Thanks everyone!
    Remember that all that code you write turns into this:

    0100100100110010010011100100111001001
    0010100100100001001111100010010010010 ....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  2. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM