determining size of pointed to array

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

Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: determining size of pointed to array

 
2
  #11
May 15th, 2009
>you can do also
You say "also", but that's identical to the solution I mentioned.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 109
Reputation: winrawr is an unknown quantity at this point 
Solved Threads: 1
winrawr's Avatar
winrawr winrawr is offline Offline
Junior Poster

Re: determining size of pointed to array

 
-1
  #12
May 17th, 2009
Originally Posted by Aia View Post
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.
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
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 22
Reputation: neigyl_noval is an unknown quantity at this point 
Solved Threads: 5
neigyl_noval neigyl_noval is offline Offline
Newbie Poster

Re: determining size of pointed to array

 
0
  #13
May 17th, 2009
I agree to winrawr at post # 12:

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,
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,048
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 179
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: determining size of pointed to array

 
0
  #14
May 17th, 2009
winrawr> I see no problem with contributing something to a solved thread,[...]
"contributing something" is the key that you are overlooking concerning my comment.
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 22
Reputation: neigyl_noval is an unknown quantity at this point 
Solved Threads: 5
neigyl_noval neigyl_noval is offline Offline
Newbie Poster

Re: determining size of pointed to array

 
0
  #15
May 17th, 2009
Ok, let's just calm down...We are not here to argue...
We are here, as a member, to help those who have
problems...
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,048
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 179
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: determining size of pointed to array

 
0
  #16
May 17th, 2009
neigyl_noval> Ok, let's just calm down...We are not here to argue...

"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,669
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 123
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: determining size of pointed to array

 
1
  #17
May 18th, 2009
Originally Posted by neigyl_noval View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: determining size of pointed to array

 
2
  #18
May 18th, 2009
>i'm here for the smackdown.
If you come for the smackdown, don't be surprised when you get smacked.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,669
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 123
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: determining size of pointed to array

 
0
  #19
May 18th, 2009
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 8
Reputation: nalply is an unknown quantity at this point 
Solved Threads: 1
nalply's Avatar
nalply nalply is offline Offline
Newbie Poster

Re: determining size of pointed to array

 
0
  #20
May 22nd, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1430 | Replies: 23
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC