943,754 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 2356
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
May 14th, 2009
0

determining size of pointed to array

Expand Post »
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.
Similar Threads
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
May 14th, 2009
3

Re: determining size of pointed to array

>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)
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 14th, 2009
0

Re: determining size of pointed to array

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.
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
May 14th, 2009
0

Re: determining size of pointed to array

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++); ?
Reputation Points: 19
Solved Threads: 1
Junior Poster
winrawr is offline Offline
110 posts
since Dec 2008
May 14th, 2009
2

Re: determining size of pointed to array

Good point. I was assuming jephthah wanted the capacity of an array, but that might have been an unwarranted assumption.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 14th, 2009
0

Re: determining size of pointed to array

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.
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
May 14th, 2009
0

Re: determining size of pointed to array

alright, just throwing it out there
Reputation Points: 19
Solved Threads: 1
Junior Poster
winrawr is offline Offline
110 posts
since Dec 2008
May 14th, 2009
0

Re: determining size of pointed to array

s'cool. thanks
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
May 15th, 2009
1

Re: determining size of pointed to array

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. }
Reputation Points: 11
Solved Threads: 0
Newbie Poster
lolguy is offline Offline
12 posts
since Apr 2009
May 15th, 2009
1

Re: determining size of pointed to array

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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006

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