943,898 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 2356
  • C RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
May 15th, 2009
2

Re: determining size of pointed to array

>you can do also
You say "also", but that's identical to the solution I mentioned.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 17th, 2009
-1

Re: determining size of pointed to array

Click to Expand / Collapse  Quote originally posted by Aia ...
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.
Clearly he has duplicated Narue's answer,
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.
Reputation Points: 19
Solved Threads: 1
Junior Poster
winrawr is offline Offline
110 posts
since Dec 2008
May 17th, 2009
0

Re: determining size of pointed to array

I agree to winrawr at post # 12:

Quote ...
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?
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,
Reputation Points: 12
Solved Threads: 5
Light Poster
neigyl_noval is offline Offline
32 posts
since May 2009
May 17th, 2009
0

Re: determining size of pointed to array

winrawr> I see no problem with contributing something to a solved thread,[...]
"contributing something" is the key that you are overlooking concerning my comment.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
May 17th, 2009
0

Re: determining size of pointed to array

Ok, let's just calm down...We are not here to argue...
We are here, as a member, to help those who have
problems...
Reputation Points: 12
Solved Threads: 5
Light Poster
neigyl_noval is offline Offline
32 posts
since May 2009
May 17th, 2009
0

Re: determining size of pointed to array

neigyl_noval> Ok, let's just calm down...We are not here to argue...

Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
May 18th, 2009
1

Re: determining size of pointed to array

We are not here to argue...
We are here, as a member, to help those who have
problems...
speak for yourself.

i'm here for the smackdown.
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
May 18th, 2009
2

Re: determining size of pointed to array

>i'm here for the smackdown.
If you come for the smackdown, don't be surprised when you get smacked.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 18th, 2009
0

Re: determining size of pointed to array

Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
May 22nd, 2009
0

Re: determining size of pointed to array

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).

  1. struct example {
  2. unsigned int len;
  3. char string[1];
  4. }

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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
nalply is offline Offline
8 posts
since May 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Using sprintf
Next Thread in C Forum Timeline: strlen implementation from scratch





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


Follow us on Twitter


© 2011 DaniWeb® LLC