| | |
determining size of pointed to array
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
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 |
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory dynamic fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest kernel km linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming stack standard string strings structures systemcall testautomation turboc unix user variable voidmain() wab win32api windows.h






