×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Incrementing and reseting a pointer

Incrementing and reseting a pointer

Incrementing and reseting a pointer

(OP)
Hello,

I have a structure and a pointer to point to this structure on the heap.

I am incrementing the pointer to add new data into the structure. After I want to display all the records in this structure. However, once the pointer has been incremented, is there a way that I can reset it to point to the first record in the structure?

My code is below:

CODE

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef struct
{
    char name[20];
    char city[20];
    char state[10];
}record;

typedef record *pointerRecord;

int main(int argc, char** argv)
{
    int i = 0;
    //Allocate meory for 2 records
    pointerRecord ptrRecord = (pointerRecord) malloc(sizeof(record[2]));

    //Fill struct with data
    strncpy(ptrRecord->name, "steve", sizeof(ptrRecord->name));
    strncpy(ptrRecord->city, "Reading", sizeof(ptrRecord->city));
    strncpy(ptrRecord->state, "Bracknell", sizeof(ptrRecord->state));

    ptrRecord++;
    strncpy(ptrRecord->name, "Robert", sizeof(ptrRecord->name));
    strncpy(ptrRecord->city, "Tibury", sizeof(ptrRecord->city));
    strncpy(ptrRecord->state, "Berks", sizeof(ptrRecord->state));

    //Display contents of the structure.
    //Reset pointer HERE to point to the first structure.    
    while(ptrRecord != 0)
    {
        printf("Name: %s", ptrRecord->name);
        printf("City: %s", ptrRecord->city);
        printf("State: %s", ptrRecord->state);
        //Increment pointer HERE
    }

    free(ptrRecord);

    getchar();

    return 0;
}

RE: Incrementing and reseting a pointer

Use an incremented variable that you add to the original pointer,

CODE

pointerRecord ptrRecord = (pointerRecord) malloc(sizeof(record[2]));
int inc = 0;

    //Fill struct with data
    strncpy((ptrRecord + inc)->name, "steve", sizeof(ptrRecord->name));
    strncpy((ptrRecord + inc)->city, "Reading", sizeof(ptrRecord->city));
    strncpy((ptrRecord + inc)->state, "Bracknell", sizeof(ptrRecord->state));

    inc += sizeof(record[2]);
    strncpy((ptrRecord + inc)->name, "Robert", sizeof(ptrRecord->name));
    strncpy((ptrRecord + inc)->city, "Tibury", sizeof(ptrRecord->city));
    strncpy((ptrRecord + inc)->state, "Berks", sizeof(ptrRecord->state));


 or create a second pointer that you increment while retaining the original value in the first pointer.

CODE

pointerRecord ptrRecord = (pointerRecord) malloc(sizeof(record[2]));
pointerRecord origPtrRecord = ptrRecord;

    //Fill struct with data
    strncpy(ptrRecord->name, "steve", sizeof(ptrRecord->name));
    strncpy(ptrRecord->city, "Reading", sizeof(ptrRecord->city));
    strncpy(ptrRecord->state, "Bracknell", sizeof(ptrRecord->state));

    ptrRecord++;
    strncpy(ptrRecord->name, "Robert", sizeof(ptrRecord->name));
    strncpy(ptrRecord->city, "Tibury", sizeof(ptrRecord->city));
    strncpy(ptrRecord->state, "Berks", sizeof(ptrRecord->state));

/* original value retained in origPtrRecord */

Lee

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close