954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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
Administrator
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
 

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++); ?

winrawr
Junior Poster
110 posts since Dec 2008
Reputation Points: 19
Solved Threads: 1
 

Good point. I was assuming jephthah wanted the capacity of an array, but that might have been an unwarranted assumption.

Narue
Bad Cop
Administrator
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
 

alright, just throwing it out there

winrawr
Junior Poster
110 posts since Dec 2008
Reputation Points: 19
Solved Threads: 1
 

s'cool. thanks :)

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

you can do also

void get_size(char *buffer,int size)
{
       fgets(buffer,size,stdin);
}
int main(void)
{
       char buffer[100];
       get_size(buffer,sizeof buffer);
}
lolguy
Newbie Poster
12 posts since May 2009
Reputation Points: 11
Solved Threads: 0
 

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
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
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?

winrawr
Junior Poster
110 posts since Dec 2008
Reputation Points: 19
Solved Threads: 1
 

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,

neigyl_noval
Light Poster
32 posts since May 2009
Reputation Points: 12
Solved Threads: 5
 

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
 

Ok, let's just calm down...We are not here to argue...
We are here, as a member, to help those who have
problems...

neigyl_noval
Light Poster
32 posts since May 2009
Reputation Points: 12
Solved Threads: 5
 

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
Administrator
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
 

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

struct example {
  unsigned int len;
  char string[1];
}


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.

nalply
Newbie Poster
8 posts since May 2009
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You