| | |
pointer assignment
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2008
Posts: 570
Reputation:
Solved Threads: 93
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:
note that
What was the variable
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:
c Syntax (Toggle Plain Text)
char *glblclrtab; glblclrtab= malloc(3*tmp); for (int i=0; i < tmp; i++) { glblclrtab[i] = fgetc( ipFile); glblclrtab[i + tmp] = fgetc( ipFile); glblclrtab[i + 2 * tmp] = fgetc( ipFile); } for (int i = 0; i < tmp; i++) { printf("%d::%d, %d, %d \n", i,glblclrtab[i], glblclrtab[tmp + i],glblclrtab[2*tmp+i]); } 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? •
•
Join Date: Dec 2008
Posts: 8
Reputation:
Solved Threads: 0
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.
•
•
Join Date: May 2008
Posts: 570
Reputation:
Solved Threads: 93
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
This is cleaner:
now that i and clrcnt always have the same value:
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)
int clrcnt = 0; for (int i=0; i<(3*tmp); i++) { if (tmp2==3) { tmp2=0; clrcnt++; } i1=fgetc( ipFile ); if (tmp2==0) { *(glblclrtab + clrcnt) = (char)i1; } else if (tmp2==1) { *(glblclrtab + tmp +clrcnt) = (char)i1; } else if (tmp2==2) { *(glblclrtab + 2*tmp + clrcnt)= (char)i1; } tmp2++; }
This is cleaner:
c++ Syntax (Toggle Plain Text)
int clrcnt = 0; for (int i=0; i<tmp; i++) { i1=fgetc( ipFile ); *(glblclrtab + clrcnt) = (char)i1; i1=fgetc( ipFile ); *(glblclrtab + tmp +clrcnt) = (char)i1; i1=fgetc( ipFile ); *(glblclrtab + 2*tmp + clrcnt)= (char)i1; clrcnt++; }
now that i and clrcnt always have the same value:
c++ Syntax (Toggle Plain Text)
for (int clrcnt=0; clrcnt<tmp; clrcnt++) { i1=fgetc( ipFile ); *(glblclrtab + clrcnt) = (char)i1; i1=fgetc( ipFile ); *(glblclrtab + tmp +clrcnt) = (char)i1; i1=fgetc( ipFile ); *(glblclrtab + 2*tmp + clrcnt)= (char)i1; }
Last edited by Murtan; Dec 23rd, 2008 at 5:56 pm.
•
•
•
•
Originally Posted by Murtan
note that *(glblclrtab + i) is functionally equivalent to glblclrtab[i] but the second is way easier to read.
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
> 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.
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.
![]() |
Similar Threads
- Error:Null Pointer assignment (C++)
- getting null pointer assignment error (C)
- In which case "Null Pointer Assignment" will be displayed on the output ?? (C)
- Why this C code is returning Null Pointer Assignment as output (C)
- Assign content to a function pointer (C)
Other Threads in the C Forum
- Previous Thread: upnp related project topics
- Next Thread: Please help me
| Thread Tools | Search this Thread |
* ansi api append array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlogicaldrivestrin givemetehcodez grade graphics gtkwinlinux histogram homework i/o ide inches include infiniteloop initialization input intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft multi mysql oddnumber open opendocumentformat openwebfoundation overwrite pdf pointer pointers posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string strings suggestions test testautomation threads unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi






