| | |
determining size of pointed to array
Thread Solved |
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...
assume the string being pointed to is properly declared and of a significant length.
.
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...
c Syntax (Toggle Plain Text)
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.
.
Last edited by jephthah; May 14th, 2009 at 3:32 pm.
>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:
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:
c Syntax (Toggle Plain Text)
void myfunction(char * pointerToString, size_t size)
I'm here to prove you wrong.
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
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
•
•
Join Date: Apr 2009
Posts: 12
Reputation:
Solved Threads: 0
you can do also
C Syntax (Toggle Plain Text)
void get_size(char *buffer,int size) { fgets(buffer,size,stdin); } int main(void) { char buffer[100]; get_size(buffer,sizeof buffer); }
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.
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.
![]() |
Similar Threads
- get length of a dynamic array (C++)
- size of integer array (C++)
- string array size (C++)
- Max size of Array (C++)
- Array of pointers (C++)
- works for static need help to make it dynamic (C++)
- how to increase the size of an array? (C)
Other Threads in the C Forum
- Previous Thread: Using sprintf
- Next Thread: strlen implementation from scratch
| Thread Tools | Search this Thread |
#include adobe ansi api array asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork forloop frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest histogram i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue mysql number odf opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape socket socketprograming standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi






