![]() |
| ||
| memory allocation ptr to array? how? hi people, i am having some problems, perhaps understanding about how dynamic memory allocation works with pointers and arrays. Im trying to get the user to input the size of the string in my program and allocates a certain amount of memory to hold the string of the size. Next, i am going to ask the user to input a string which must consists of only M, U, I. Im going to put that into my input array. The problem now is how am i going to reference the memory pointer to the input array ? Also im having a problem with functions too. My isaMUstring function has to return a 1, if the user input correctly ( only M, U, I) and a 0 if otherwise. I can't seem to get the concept of getting the return value of the function to work in my main function. If i can get the the return value of my function, i am able to perform other task like if the function return a 1, i would print "Correct", if otherwise, i would print "Incorrect". I have tried if( isaMUstring( 1 ) ){ do someting} in the main. but it doesn't work. Could someone offer some help on the above 2 problems? Below is a sample of my code. Thanks in advance. #include <stdio.h> cross posted in : http://cboard.cprogramming.com/newth...newthread&f=4# << moderator edit: fixed code tags -- and thanks for mentioning the cross-post >> |
| ||
| Re: memory allocation ptr to array? how? Hi there .. i think i have read something like this here: try this link http://pweb.netcom.com/~tjensen/ptr/pointers.htm |
| ||
| Re: memory allocation ptr to array? how? I think strspn may be a better choice for your validator. First you ask for the size of the array, and that was fine. Then you want to allocate space to a pointer, adding one to make room for the terminating null. Then use the pointer much like an array. #include <stdio.h>Note that using scanf with %s (no specified size) is not a good idea. But the fixes bring other issues into this, so I'll pass on stuff like that for now. [edit]Or this snippet or two may be useful. |
| ||
| Re: memory allocation ptr to array? how? Wouldn't it be safer to use fgets() rather then scanf()? |
| All times are GMT -4. The time now is 6:51 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC