| | |
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)
New members chased away this month: 5
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.
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
![]() |
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
Views: 1434 | Replies: 23
| Thread Tools | Search this Thread |
Tag cloud for C
* api append array arrays bash binarysearch changingto char character cm copyanyfile copypdffile createcopyoffile createprocess() csyntax database directory drawing dynamic executable execv feet fgets file floatingpointvalidation fork frequency function getlogicaldrivestrin givemetehcodez global graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping lowest matrix meter microsoft mqqueue mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked reversing scheduling segmentationfault send single socketprogramming spoonfeeding standard strchr string student suggestions system test testautomation testing unix urboc user whythiscodecausesegmentationfault win32api windowsapi






