Thread: Reset a pointer

  1. #1
    UK2
    Join Date
    Sep 2003
    Posts
    112

    Reset a pointer

    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;
    }

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Just store a copy of the original pointer in another variable for use at the end.

  3. #3
    UK2
    Join Date
    Sep 2003
    Posts
    112
    Thanks brewbuck that worked. Solution below.

    However, I have noticed that when I try to get to the end of the structure when I am printing it will display a lot of rubbish.
    Code:
    while(ptrRecord != 0)
    I was thinking the above line would get to the last record and break out of the while loop. However, it doesn't break and keeps on looping. Is there any other condition to set this loop too.

    Thanks,


    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]));
    	pointerRecord resetPointer;
    	resetPointer = ptrRecord; //Will point to the orginal address
    
    	//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.	
    	ptrRecord = resetPointer;
    	while(ptrRecord != 0)
    	{
    		printf("Name: &#37;s\n", ptrRecord->name);
    		printf("City: %s\n", ptrRecord->city);
    		printf("State: %s\n", ptrRecord->state);
    		//Increment pointer HERE
    		ptrRecord++;
    	}
    
    	free(ptrRecord);
    
    	getchar();
    
    	return 0;
    }

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you know the count of mebers in the array
    use for loop with index
    for(i=0;i<array_size;i++)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    typedef record *pointerRecord;
    Pointer typedefs obfuscates the code. Better not to use them. Don't be lazy and type record* instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    100
    Er... Instead of:

    Code:
    	
             //Allocate meory for 2 records
    	pointerRecord ptrRecord = (pointerRecord) malloc(sizeof(record[2]));
    maybe

    Code:
    	//Allocate meory for 2 records
    	pointerRecord ptrRecord = (pointerRecord) malloc(2 * sizeof(record));

  7. #7
    UK2
    Join Date
    Sep 2003
    Posts
    112
    Hello,

    Just wondering what is the difference between writing
    Code:
    malloc(sizeof(record[2]));
    and
    Code:
    malloc(2 * sizeof(rocord));
    I have never seen the second method been used before. And advantages?

    Thanks,

    Steve

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    1) Allocates 1 x size of element 3 in variable record (bad).
    2) Allocates 2 x size of record.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    no, record is a type. record[2] is type array of records with 2 elements. They are both functionally identical. If you don't have any optimizing at all in a very non optimizing compiler, the first one might avoid a multiplication, but don't quote me on that.

    If 2 * sizeof(record) overflows the size of size_t, you'd be more likely to receive a compile-time error message, I think. But don't quote me on that either.
    Last edited by robwhit; 04-26-2008 at 11:25 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  2. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 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 validity check
    By Carlos in forum Windows Programming
    Replies: 6
    Last Post: 12-11-2003, 03:40 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM