determining size of pointed to array

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

Join Date: Feb 2008
Posts: 1,661
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

determining size of pointed to array

 
0
  #1
May 14th, 2009
im going to risk getting a beating, because this is something i should (used to?) know...

anyhoo, given a function, how do you tell the size of the array that is passed? obviously trying to find the sizeof the pointer doesn't work (it returns the size of the pointer, duh)

but im having a brainfart trying to remember how to find the size of the passed array...

  1. void myFunction (char * myString)
  2. {
  3. size_t len = sizeof(myString)
  4. // gives size of pointer, not string
  5.  
  6. }

assume the string being pointed to is properly declared and of a significant length.

.
Last edited by jephthah; May 14th, 2009 at 3:32 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,783
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: 745
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: determining size of pointed to array

 
3
  #2
May 14th, 2009
>given a function, how do you tell the size of the array that is passed?
Given a function where the only parameter is a pointer to the first element of said array, you don't. There's no portable method, which is why the usual advice is a second parameter with the size:
  1. void myfunction(char * pointerToString, size_t size)
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,661
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
  #3
May 14th, 2009
er. i was hoping that wasnt the only answer.

oh, well, it's not a big deal. i just thoguht there was some clever trick that i had forgotten.
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

 
0
  #4
May 14th, 2009
okay wait, even though this thread is solved, if it's a string couldn't you use strlen? Or just int length = 0; while(length++,*myString++); ?
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: Sep 2004
Posts: 7,783
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: 745
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: determining size of pointed to array

 
2
  #5
May 14th, 2009
Good point. I was assuming jephthah wanted the capacity of an array, but that might have been an unwarranted assumption.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,661
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
  #6
May 14th, 2009
no, i dont want a length of some arbitrary string, that may or may not be null-terminated

Narue was right: i wanted the size of the entire array.
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

 
0
  #7
May 14th, 2009
alright, just throwing it out there
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: Feb 2008
Posts: 1,661
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
  #8
May 14th, 2009
s'cool. thanks
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 12
Reputation: lolguy is an unknown quantity at this point 
Solved Threads: 0
lolguy lolguy is offline Offline
Newbie Poster

Re: determining size of pointed to array

 
1
  #9
May 15th, 2009
you can do also
  1. void get_size(char *buffer,int size)
  2. {
  3. fgets(buffer,size,stdin);
  4. }
  5. int main(void)
  6. {
  7. char buffer[100];
  8. get_size(buffer,sizeof buffer);
  9. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,039
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: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: determining size of pointed to array

 
1
  #10
May 15th, 2009
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.
Last edited by Aia; May 15th, 2009 at 11:01 am.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC