determining size of pointed to array
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...
void myFunction (char * myString)
{
size_t len = sizeof(myString)
// gives size of pointer, not string
}
assume the string being pointed to is properly declared and of a significant length.
.
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
>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:
void myfunction(char * pointerToString, size_t size)
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
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.
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
Good point. I was assuming jephthah wanted the capacity of an array, but that might have been an unwarranted assumption.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
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.
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
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.
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
>you can do also
You say "also", but that's identical to the solution I mentioned. :icon_rolleyes:
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
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
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
neigyl_noval> Ok, let's just calm down...We are not here to argue...
:-/ :?: :icon_rolleyes: :yawn:
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
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.
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
>i'm here for the smackdown.
If you come for the smackdown, don't be surprised when you get smacked.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179