943,929 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2602
  • C RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Dec 23rd, 2008
0

Re: pointer assignment

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?
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Dec 23rd, 2008
0

Re: pointer assignment

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
m24r is offline Offline
8 posts
since Dec 2008
Dec 23rd, 2008
0

Re: pointer assignment

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
c++ Syntax (Toggle Plain Text)
  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:
c++ Syntax (Toggle Plain Text)
  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:
c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Dec 24th, 2008
0

Re: pointer assignment

I was late a posting so deleting this post>
<deleted>
Last edited by Luckychap; Dec 24th, 2008 at 1:38 am.
Reputation Points: 83
Solved Threads: 61
Posting Pro in Training
Luckychap is offline Offline
442 posts
since Aug 2006
Dec 24th, 2008
0

Re: pointer assignment

Quote 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 piss 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.
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Dec 24th, 2008
0

Re: pointer assignment

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.
Reputation Points: 124
Solved Threads: 18
Junior Poster
devnar is offline Offline
148 posts
since Sep 2008
Dec 24th, 2008
0

Re: pointer assignment

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
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Dec 25th, 2008
1

Re: pointer assignment

> 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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Dec 25th, 2008
0

Re: pointer assignment

Sorry about that. I made a quick program to test it and you're right Selem.
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: upnp related project topics
Next Thread in C Forum Timeline: Please help me





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC