| | |
determining size of pointed to array
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
•
•
lolguy> you can do also
Did you read post number two before?
Did you notice that the thread is marked as solved? Which unless you are sure something has been overlooked, you should learn to leave alone.
however, I don't agree with your second point...
I see no problem with contributing something to a solved thread, especially if there's more than one way to do it. If the OP doesn't need any other way to do it, that's fine, alternative answers can be a benefit to other readers of a thread. I often click on solved links to read about a problem and its solutions to learn something about it--and it makes it all the more beneficial if there are lots of different fixes to it. I don't stop reading after the first acceptable answer, so why stop posting solutions after the first?
Last edited by winrawr; May 17th, 2009 at 3:56 am.
I wake up! And my mind's out, never again will I sell out. Converting vegetarians.
Into the midnight giving it to you, I don't know it just feels right.
This is the time of the revolution, Cooking the next step.
Converting vegetarians, minding the gap since 1996
Into the midnight giving it to you, I don't know it just feels right.
This is the time of the revolution, Cooking the next step.
Converting vegetarians, minding the gap since 1996
•
•
Join Date: May 2009
Posts: 22
Reputation:
Solved Threads: 5
I agree to winrawr at post # 12:
But, if
this "iterating" posts have really been normed as discouraged
(I know it's in the rules), so be it. I would rather keep quiet in
order to help people in need rather than to be kicked out and
could not help other people anymore...
I'm sorry for this pathetic post,
•
•
•
•
I see no problem with contributing something to a solved thread, especially if there's more than one way to do it. If the OP doesn't need any other way to do it, that's fine, alternative answers can be a benefit to other readers of a thread. I often click on solved links to read about a problem and its solutions to learn something about it--and it makes it all the more beneficial if there are lots of different fixes to it. I don't stop reading after the first acceptable answer, so why stop posting solutions after the first?
this "iterating" posts have really been normed as discouraged
(I know it's in the rules), so be it. I would rather keep quiet in
order to help people in need rather than to be kicked out and
could not help other people anymore...
I'm sorry for this pathetic post,
Back to the topic. I have another solution.
The capacity of the array must be known to the function. If the capacity is not hardcoded or saved in a global variable, you need to pass it to the function. Narue proposed to pass it in a second parameter.
Another solution is to make a bundle of the array and its capacity. A cheap, dirty trick is misusing the first array element for it. For strings, the first byte is the string length (lengths from 0 to 255 are possible), for integer array, the first integer is the array length, etc. For non-char or non-integer array, a cast would be neccessary. But this is very dirty and dangerous. Pascal-type strings do this implicitly, which is fine because Pascal hides this trick.
A third solution is to create a struct both of capacity and an array of length 1 (or 0 with gcc).
Allocate dynamically with
This works, because C does not check the array bounds. When we say
The capacity of the array must be known to the function. If the capacity is not hardcoded or saved in a global variable, you need to pass it to the function. Narue proposed to pass it in a second parameter.
Another solution is to make a bundle of the array and its capacity. A cheap, dirty trick is misusing the first array element for it. For strings, the first byte is the string length (lengths from 0 to 255 are possible), for integer array, the first integer is the array length, etc. For non-char or non-integer array, a cast would be neccessary. But this is very dirty and dangerous. Pascal-type strings do this implicitly, which is fine because Pascal hides this trick.
A third solution is to create a struct both of capacity and an array of length 1 (or 0 with gcc).
C Syntax (Toggle Plain Text)
struct example { unsigned int len; char string[1]; }
Allocate dynamically with
malloc(sizeof(struct example) + (len - 1) * sizeof(char)) (not tested). Drop -1 if using string[0].This works, because C does not check the array bounds. When we say
string[2] , the C compiler won't report errors, and because we allocated memory for it, it will work. ![]() |
Similar Threads
- get length of a dynamic array (C++)
- size of integer array (C++)
- string array size (C++)
- Max size of Array (C++)
- Array of pointers (C++)
- works for static need help to make it dynamic (C++)
- how to increase the size of an array? (C)
Other Threads in the C Forum
- Previous Thread: Using sprintf
- Next Thread: strlen implementation from scratch
Views: 1430 | Replies: 23
| Thread Tools | Search this Thread |
Tag cloud for C
#include ansi array arrays asterisks binarysearch calculate centimeter changingto char command convert copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic fflush file fork forloop framework functions getlasterror givemetehcodez grade graphics gtkgcurlcompiling hacking hardware histogram homework inches include incrementoperators input iso kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard mysql number opendocumentformat opensource owf pattern pdf performance pointer posix problem probleminc process program programming radix recursion recv research reversing scanf scripting segmentationfault sequential shape socket socketprograming spoonfeeding standard string strings structures student systemcall testing threads turboc unix user variable voidmain() wab windows.h windowsapi






