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 support@ccsinfo.com

continued discussion about structure pointer problem

 
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

continued discussion about structure pointer problem
PostPosted: Wed Jan 11, 2006 4:42 am     Reply with quote

Dear all,

I raised a question about a pointer problem in structure. I think I still have some confusions over there not resolved and would like to seek further help here.

The original post is:
http://www.ccsinfo.com/forum/viewtopic.php?t=25600&highlight=userrecord

My further question is:

I acknowledge that the structure pointers in the original post were not assigned proper addresses initially and they ended up pointing at successive word addresses which caused interferances.

However, in my code, I did exactly the same as the suggested solution which didn't work on me! The code is kinda like:



Code:
struct node
{   
   int tag;
   char UserID[9];
   char Password[9];
   int admin_index;
   int TestNo;
};
struct node* UserRecord[10];
struct node User[10];

void main()
{

   for(i=0;i<10;i++)
      UserRecord[i] = &User[i];
....
....
....

}



I actually have another soltion which is:

Code:
struct node
{   
   int tag;
   char UserID[9];
   char Password[9];
   int admin_index;
   int TestNo;
};


struct node *UserRecord[10];


void main()
{

   UserRecord[0] = malloc( sizeof(struct node ) );   
   for(i=1;i<10;i++)
   {
      UserRecord[i] = malloc( sizeof(struct node ) );
      UserRecord[i] = UserRecord[i-1] + sizeof(struct node) ;
      
   }
....
....
....

}


I thought it should have worked because all the pointers were assigned a proper address with a proper length of separation in memory. However it didn't work again.

what on earth is going wrong over there?
asmallri



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Wed Jan 11, 2006 7:49 am     Reply with quote

I expected the first method to work.

What exactly happens when it does not work?

Here is another (cleaner?) way.


Code:
struct node
{   
   int tag;
   char UserID[9];
   char Password[9];
   int admin_index;
   int TestNo;
};
struct node* UserRecord[10];


void main()
{

   for(i=0;i<10;i++)
      UserRecord[i] = malloc(sizeof(struct node));
....
....
....

}

_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
Ttelmah
Guest







PostPosted: Wed Jan 11, 2006 8:06 am     Reply with quote

I think the reason the malloc version would fail, is this line:
UserRecord[i] = UserRecord[i-1] + sizeof(struct node) ;

This is destroying the value already put into the pointer by the line before, and is making the assumption, that the memory blocks will be consecutive, and in order......

I would have expected the original version to work. I use structure pointers like this fairly frequently, and have not seen any problems.

Best Wishes
ye



Joined: 11 May 2005
Posts: 57
Location: london

View user's profile Send private message

PostPosted: Wed Jan 11, 2006 9:11 am     Reply with quote

Thanks for your replies.

The aftermath of using the first method in my code was exactly the same as that described in the first post, i.e., when assigning a different string to a user record, contents of other user records get interferred.

Telmah, so are you saying when I am using malloc, the allocated memory location is randomly given and I would spoil the code if I add an additional statement trying to fix the memory blocks in order like
'UserRecord[i] = UserRecord[i-1] + sizeof(struct node) ; ' ?
treitmey



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

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

pointer to a struct
PostPosted: Wed Jan 11, 2006 10:41 am     Reply with quote

I went through something like this before. Mark helped me out
It is order of eval and type casting the pointer to a 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
#define NUM_USER_REC 15
struct node
{
  char tag;
  char UserID[9];
  char Password[9];
  char admin_index;
};   // Note this is not a node in linked list
struct node UserRecord[NUM_USER_REC],*ptrUserRecord[NUM_USER_REC];

//=================prototypes=================//
print_user(struct node *ptrUserRecord);

//======================= MAIN ============================//
void main(void)
{
  char j,i,k;
  //================================================
  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");
  for(i=0;i<NUM_USER_REC;i++){
    ptrUserRecord[i]=&UserRecord[i];//assign pointer to address of struct
  }
  for(i=0;i<NUM_USER_REC;i++){//assign tag the value of index * 3
    UserRecord[i].tag=i*3;
  }
  for(i=0;i<NUM_USER_REC;i++){//assign admin_index a value
    UserRecord[i].admin_index=i+5;
  }
   for(i=0;i<NUM_USER_REC;i++){//assign UserID a value
    sprintf(UserRecord[i].UserID,"Tim%u",i);
  }
  for(i=0;i<NUM_USER_REC;i++){//assign Password a value
    sprintf(UserRecord[i].Password,"pass%u",i*2);
  }

  //---------------print the users-----------------//
  for(i=0;i<NUM_USER_REC;i++){//print the users
    print_user((struct node*)ptrUserRecord[i]);
  }
  //-----------------------------------------------//

//  fprintf(DEBUG,"Tag=%u, ",((struct node*)ptrUserRecord[0])->tag);
//  fprintf(DEBUG,"UserID=%s, ",((struct node*)ptrUserRecord[0])->UserID);
//  //--
//  fprintf(DEBUG,"Tag=%u, ",((struct node*)ptrUserRecord[1])->tag);
//  fprintf(DEBUG,"UserID=%s, ",((struct node*)ptrUserRecord[1])->UserID);
//  //--
//  fprintf(DEBUG,"Tag=%u, ",((struct node*)ptrUserRecord[2])->tag);
//  fprintf(DEBUG,"UserID=%s, ",((struct node*)ptrUserRecord[2])->UserID);

  fprintf(DEBUG,"DONE!\n\r");
  while(1)
  {
  }
}
//=================print_user()=============================//
print_user(struct node* ptr)
{
  char x;
  fprintf(DEBUG,"Tag=%u\n\r",(struct node*)ptr->tag);
  fprintf(DEBUG,"UID=%s\n\r",(struct node*)ptr->UserID);
  fprintf(DEBUG,"Pswd=%s\n\r",(struct node*)ptr->Password);
  fprintf(DEBUG,"Adm_indx=%u\n\r\n\r",(struct node*)ptr->admin_index);
  return;
}



//fprintf(DEBUG,"%X \n\r", ((struct pkt*)p_pkt[i])->data[1]);
ye



Joined: 11 May 2005
Posts: 57
Location: london

View user's profile Send private message

PostPosted: Wed Jan 11, 2006 10:55 am     Reply with quote

treitmey, thanks for the demo.

Maybe it's not a good question, but for function print_user, since parameter 'ptr' has been defined as 'struct node *' in function definition, then why do you add an additional type casting at it whenever you use it inside the function body?

Also, in the demo, all operations were done with UserRecord[i] and I can't see the meaning of using pointers here. Could you explain please ?

Cheers
treitmey



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

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

PostPosted: Wed Jan 11, 2006 10:58 am     Reply with quote

Not sure if that is required, but i think it need to be in there one of the two places.

The pointers are just giving you options. Like in the commented out printf statments. The fuction uses a pointer to a struct so that it can know the definition of the data types in the struct node.

In main I could have simply passed an address of the struct... &userrecord[i]
to my print_user function.


I'm not sure if i'm answering your question.
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