some basic questions about pointers and structs...

This is a discussion on some basic questions about pointers and structs... within the C Programming forums, part of the General Programming Boards category; i would like (if it is possible) someone to explain the program. i know that it is not a complete ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    230

    some basic questions about pointers and structs...

    i would like (if it is possible) someone to explain the program. i know that it is not a complete program but...i have the following questions...

    (1) is this the same as
    Code:
    char name[];
    ?
    if 'yes' what is the difference?

    (2) is the following a correct way of calling the function from main()?
    Code:
    int main(void){
       init(*p);
    }
    if 'yes' what is the difference of this:
    Code:
    int main(void){
       init(p);
    }
    and lastly i want to know what -> is? can i have some more details about this please, cause i don' t understand...

    Code:
    #include <stdio.h>
    
    struct Person{
       char *name; // (1)
       int age;
    };
    
    void init(struct Person *p){ // (2)
       p->name = (char *)malloc(10*sizeof(char));
       p->age = 0;
    }
    thank you all....

  2. #2
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    Pointers are a bit of a pain to explain if you ask me. To understand pointers you must realise what a variable in a computer program is - some data in memory. All a pointer is, is a variable which stores the memory address of a certain variable one of the advantages of this (among others) is improved computational performance when calling functions etc.

    When you call a function and you pass a variable to that function it makes a copy of the variable and works on the copy, but if you pass a pointer into the function any operations are directly applied to the variable in question. It is easiest to see with a few examples so here we go:
    Code:
    int FuncOne(int val)
    {
          return (2*val);
    }
    
    int FuncTwo(int * val)
    {
          return (2*(*val));
    }
    
    /* First Main Example */
    int main(int argc, char *argv[])
    {
        int val = 5.0;
        int * val_ptr = &val;
        
        val = FuncOne(val);
        val = FuncTwo(val_ptr);
        
        val = FuncOne(*val_ptr);
        val = FuncTwo(&val);
        
        printf("%d\n", val);
        
      return 0;
    }
    FuncOne and FuncTwo do exactly the same thing but they do it differently. The '*' signifies a pointer and the '&' signifies the address of the variable. I know this isn't a very thorough explanation. Imagine a variable and a pointer as boxes and inside the pointer box there is the location of the variable box and inside the variable box is the value of the variable itself. The '*' and '&' are just ways of moving from one to the other.

    The '->' operator is used to access variables defined through pointers, but I'll let someone else talk about those. I've got things to do right now.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    1) I suggest you google the differences between pointers and char arrays in C. There are a gazillion topics on this forum alone about this.

    2) -> is used to access a member of a pointer to a complex type (such as a struct).

    For example:

    struct point{
    double x;
    double y;
    };

    struct point my_point;
    /* in this case you can access my point's members with . because my_point is an object */

    my_point.x = 2.3;

    struct point* my_ptr_to_a_point;
    /* in this case you can access my_ptr_to_a_point's members with -> because it is a pointer*/

    my_ptr_to_a_point->x = 2.3;
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    char name[] is an array of chars whereas char *name points to a string literal whcih you can't change. It's read only marked by the OS.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    thank you all guys...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with arrays, pointers, and structs.
    By RexInTheCity in forum C Programming
    Replies: 5
    Last Post: 03-29-2010, 03:30 PM
  2. Structs, pointers and functions
    By osici in forum C Programming
    Replies: 2
    Last Post: 04-29-2009, 12:35 AM
  3. passing structs & pointers to structs as arguments
    By Markallen85 in forum C Programming
    Replies: 6
    Last Post: 03-16-2004, 06:14 PM
  4. pointers to pointers within structs
    By Lord_azrael99 in forum C Programming
    Replies: 2
    Last Post: 08-28-2003, 04:29 AM
  5. structs like pointers?
    By mart_man00 in forum C Programming
    Replies: 5
    Last Post: 03-14-2003, 02:16 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21