pointer assignment

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2008
Posts: 570
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 93
Murtan Murtan is offline Offline
Posting Pro

Re: pointer assignment

 
0
  #11
Dec 23rd, 2008
If I read the code right, the intent is to split up color table values?

bytes in the file come as r,g,b,r,g,b...

You want the array to contain r,r,r...g,g,g...b,b,b...

Is that close?

if so, what do you think of:
  1. char *glblclrtab;
  2. glblclrtab= malloc(3*tmp);
  3.  
  4. for (int i=0; i < tmp; i++)
  5. {
  6. glblclrtab[i] = fgetc( ipFile);
  7. glblclrtab[i + tmp] = fgetc( ipFile);
  8. glblclrtab[i + 2 * tmp] = fgetc( ipFile);
  9. }
  10.  
  11. for (int i = 0; i < tmp; i++)
  12. {
  13. printf("%d::%d, %d, %d \n",
  14. i,glblclrtab[i], glblclrtab[tmp + i],glblclrtab[2*tmp+i]);
  15. }
  16.  
  17. free (glblclrtab); // program nolonger aborts

note that *(glblclrtab + i) is functionally equivalent to glblclrtab[i] but the second is way easier to read.


What was the variable clrcnt in your code?
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 8
Reputation: m24r is an unknown quantity at this point 
Solved Threads: 0
m24r m24r is offline Offline
Newbie Poster

Re: pointer assignment

 
0
  #12
Dec 23rd, 2008
clrcnt is just a variable to segregate the RGB values. However, what I am looking at is that I am not retreiving the same values as stored. Is there a flaw in code ? I could have used array if I had know the array size beforehand. However, I get the size only when I read the file, so to save space, malloc was seemingly a good solution to me.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 570
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 93
Murtan Murtan is offline Offline
Posting Pro

Re: pointer assignment

 
0
  #13
Dec 23rd, 2008
malloc is the right solution, but you can treat a pointer to allocated memory like an array of the same type. As I said, the expressions are interchangeable, I just prefer the second.

isn't tmp the number of bytes between sections?

the code never updated clrcnt so if it was zero, all data is stored in the first entry in the table. If it was non-zero, all the data is stored at that index.

This would work
  1. int clrcnt = 0;
  2. for (int i=0; i<(3*tmp); i++)
  3. {
  4. if (tmp2==3)
  5. {
  6. tmp2=0;
  7. clrcnt++;
  8. }
  9. i1=fgetc( ipFile );
  10. if (tmp2==0) {
  11. *(glblclrtab + clrcnt) = (char)i1;
  12. }
  13. else if (tmp2==1) {
  14. *(glblclrtab + tmp +clrcnt) = (char)i1;
  15. }
  16. else if (tmp2==2) {
  17. *(glblclrtab + 2*tmp + clrcnt)= (char)i1;
  18. }
  19. tmp2++;
  20. }

This is cleaner:
  1. int clrcnt = 0;
  2. for (int i=0; i<tmp; i++)
  3. {
  4. i1=fgetc( ipFile );
  5. *(glblclrtab + clrcnt) = (char)i1;
  6. i1=fgetc( ipFile );
  7. *(glblclrtab + tmp +clrcnt) = (char)i1;
  8. i1=fgetc( ipFile );
  9. *(glblclrtab + 2*tmp + clrcnt)= (char)i1;
  10. clrcnt++;
  11. }

now that i and clrcnt always have the same value:
  1. for (int clrcnt=0; clrcnt<tmp; clrcnt++)
  2. {
  3. i1=fgetc( ipFile );
  4. *(glblclrtab + clrcnt) = (char)i1;
  5. i1=fgetc( ipFile );
  6. *(glblclrtab + tmp +clrcnt) = (char)i1;
  7. i1=fgetc( ipFile );
  8. *(glblclrtab + 2*tmp + clrcnt)= (char)i1;
  9. }
Last edited by Murtan; Dec 23rd, 2008 at 5:56 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 319
Reputation: Luckychap is on a distinguished road 
Solved Threads: 42
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Posting Whiz

Re: pointer assignment

 
0
  #14
Dec 24th, 2008
I was late a posting so deleting this post>
<deleted>
Last edited by Luckychap; Dec 24th, 2008 at 1:38 am.
When you think you have done a lot, then be ready for YOUR downfall.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: pointer assignment

 
0
  #15
Dec 24th, 2008
Originally Posted by Murtan
note that *(glblclrtab + i) is functionally equivalent to glblclrtab[i] but the second is way easier to read.
Not entirely true, it is possible that the array was broken up in memory (to save space, or maybe just to **** programmers off). The first approach will not account for that, the second will. Plus, if you used the first, you have to be careful of accounting for the size of each array element.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 146
Reputation: devnar will become famous soon enough devnar will become famous soon enough 
Solved Threads: 16
devnar's Avatar
devnar devnar is offline Offline
Junior Poster

Re: pointer assignment

 
0
  #16
Dec 24th, 2008
Originally Posted by death_oclock View Post
Not entirely true, it is possible that the array was broken up in memory
Does that ever actually happen? I thought arrays are always assigned contiguous blocks of memory.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: pointer assignment

 
0
  #17
Dec 24th, 2008
First off I need to correct myself. When doing pointer arithmatic (ie. *(glblclrtab + i)) c automatically accounts for the size of the elements. As for the other point, I have not tested it but try reading this link: CTutorial.html#Arrays%20and%20Pointers
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: pointer assignment

 
1
  #18
Dec 25th, 2008
> I thought arrays are always assigned contiguous blocks of memory.
They are.
As are all blocks of memory returned by malloc / calloc / realloc

The link posted by death_oclock in the previous post is just a load of rubbish.

They seem to think that because the OS might map parts of the array to different pages of memory, that it has any effect on a C program. It DOESN'T. The virtual address is contiguous, and that's all that matters.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: pointer assignment

 
0
  #19
Dec 25th, 2008
Sorry about that. I made a quick program to test it and you're right Selem.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC