CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Pointer problem with structure

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ye



Joined: 11 May 2005
Posts: 57
Location: london

View user's profile Send private message

Pointer problem with structure
PostPosted: Fri Jan 06, 2006 10:02 am     Reply with quote

Dear all,

happy new year!~

I am using 18f8722 and was puzzled by a problem with pointer and structure. The problem is:

I've defined a structure like :
Code:
struct node
{   
   int tag;
   char UserID[9];
   char Password[9];
   int admin_index;
};   // Note this is not a node in linked list


and define an array of pointers pointing to this structure:
Code:
struct node* UserRecord[10];


You can see I have 10 records of this type to process in the code. Initially UserRecord[i]->UserID and UserRecord[i]->Password are all blank. They then are respectively given a new content:

Code:
   j = 97;

   for(i=0;i<10;i++)
   {
      for(k=0;k<8;k++)      
         UserRecord[i]->Password[k] = j;
         
      UserRecord[i]->Password[8] = '\0';

      j++;
   }


The UserIDs and Passwords in the 10 records are assigned a different string and each assignment shouldn't have affected other records. However, it seems to me the UserIDs and Passwords are cross affected by each assignment. E.G. after the first FOR loop, if UserRecord[0]->UserID and UserRecord[0]->Password are respectively given "spsp" and "mpmpmp", the UserIDs and Passwords of all the 10 records are:

Quote:
Record 0, UserID = spsp, Password = mpmpmp
Record 1, UserID = sp, Password = mpmp
Record 2, UserID = , Password = mp
Record 3, UserID = , Password =
Record 4, UserID = , Password =
Record 5, UserID = pmpmp, Password =
Record 6, UserID = pmp, Password =
Record 7, UserID = p, Password =
Record 8, UserID = , Password =
Record 9, UserID = , Password =


Clearly, record 2,5,6,7 have been cross affected.

However, if I do not define the array as pointers,
Quote:
struct node UserRecord[10];

this wouldn't happen.

I am suspecting this is due to overflow or memory collision but I can't see how it happened. Help?
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Fri Jan 06, 2006 10:32 am     Reply with quote

Looks to me like you defined the struct and pointers but didn't instatiate the struct.
Code:
#include <18F452.H>
#case
#use delay(clock=40000000)
#fuses h4,nowdt,noprotect,nolvp
#use rs232(baud=19200,xmit=PIN_C0,invert,stream=DEBUG,disable_ints) // stderr(same as debug)
#zero_ram
//======================= MAIN ============================//
void main(void)
{
  char j,i,k;
  struct node
  {
    int tag;
    char UserID[9];
    char Password[9];
    int admin_index;
  };   // Note this is not a node in linked list
  struct node UserRecord[10],*ptrUserRecord[10];//instance and pointers

  //================================================
  setup_adc_ports(NO_ANALOGS);
  set_tris_a(0);set_tris_b(0);set_tris_c(0);
  set_tris_d(0);set_tris_e(0);
  //================================================
  fprintf(DEBUG,"STARTING.\n\r");
  ptrUserRecord[0]=&UserRecord[0];
  ptrUserRecord[1]=&UserRecord[1];
  ptrUserRecord[2]=&UserRecord[2];
  ptrUserRecord[3]=&UserRecord[3];
  ptrUserRecord[4]=&UserRecord[4];
  ptrUserRecord[5]=&UserRecord[5];
  ptrUserRecord[6]=&UserRecord[6];
  ptrUserRecord[7]=&UserRecord[7];
  ptrUserRecord[8]=&UserRecord[8];
  ptrUserRecord[9]=&UserRecord[9];


  j = 97;
  for(i=0;i<10;i++)
  {
    for(k=0;k<8;k++){
      ptrUserRecord[i]->Password[k] = j;//pass=97,97,97,97,97,97,97,97,/0
    }
    ptrUserRecord[i]->Password[8] = '\0';//string terminator
    j++;
    fprintf(DEBUG,"--%s---\n\r",ptrUserRecord[i]->Password);
  }

  fprintf(DEBUG,"DONE !\n\r");
  while(1)
  {
  }
}
Ttelmah
Guest







PostPosted: Fri Jan 06, 2006 10:40 am     Reply with quote

Yes.
I was just about to post, and you beat me to it. It actually looks as if the pointers (since they are never allocated), end up pointing to successive words in memory, making all the structures overlap...

Best Wishes
ye



Joined: 11 May 2005
Posts: 57
Location: london

View user's profile Send private message

PostPosted: Fri Jan 06, 2006 10:50 am     Reply with quote

I got you guys.

Many thanks!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group