User Tools

Site Tools


opus:fall2012:dgirard3:datapart1

Structure Pointer

Definition

A structure pointer is a pointer that points to a structure! Like any pointer,it points to an address in memory. And a structure has multiple elements in one block. Now you can pass and return these structures through functions to get the data you want but this is excessive data movement so to reduce this you would pass a pointer to the structure to the function. All we would have to do is reference the pointer to the struct then dereference it to receive the data we want.

#include <stdio.h>
#include <string.h>
 
struct tag{                     /* the structure type */
    char lname[20];             /* last name */
    char fname[20];             /* first name */
    int age;                    /* age */
    float rate;                 /* e.g. 12.75 per hour */
};
 
struct tag my_struct;           /* define the structure */
void show_name(struct tag *p);  /* function prototype */
 
int main(void)
{
    struct tag *st_ptr;         /* a pointer to a structure */
    st_ptr = &my_struct;        /* point the pointer to my_struct */
    strcpy(my_struct.lname,"Jensen");
    strcpy(my_struct.fname,"Ted");
    printf("
%s ",my_struct.fname);
    printf("%s
",my_struct.lname);
    my_struct.age = 63;
    show_name(st_ptr);          /* pass the pointer */
    return 0;
}
 
void show_name(struct tag *p)
{
    printf("
%s ", p->fname);  /* p points to a structure */
    printf("%s ", p->lname);
    printf("%d
", p->age);
}

References

data Keyword 1 Phase 2

Null Pointers

Definition

A Null pointer is a pointer that points definitively nowhere. It is a special pointer that is used to symbolize that the pointer is pointing at nothing or that there is nothing there. The null pointer is commonly used in malloc, when it fails it returns a null pointer because nothing happened so therefore you get nothing in return.

References

None on this one :)

Demonstration

There is no real way to demonstrate this, if this was compiled in a program it will just come up as undefined or 0 or NULL.

opus/fall2012/dgirard3/datapart1.txt · Last modified: 2012/10/01 03:09 by dgirard3