| | |
memory allocation ptr to array? how?
![]() |
•
•
Join Date: Aug 2005
Posts: 13
Reputation:
Solved Threads: 0
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.
cross posted in : http://cboard.cprogramming.com/newth...newthread&f=4#
<< moderator edit: fixed code tags -- and thanks for mentioning the cross-post >>
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.
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <string.h> int isaMUstring( const char *string ); int main(){ int size; char input[20]; char *ptr; printf("Enter the size of string."); scanf("%d",&size); ptr = (char *)malloc((size +1) * sizeof(char *)); if( ptr) { printf("Enter a character string(consist of 'M', 'I', 'U' only) :\n"); scanf("%s",&input); isaMUstring( input ); } return 0; } int isaMUstring( const char *string ) { const char *cha1 = "M"; const char *cha2 = "I"; const char *cha3 = "U"; int a; a = 0; if( strpbrk( string, cha1) != NULL) { a += 1; } if( strpbrk( string, cha2) != NULL) { a += 1; } if( strpbrk( string, cha3 ) != NULL) { a += 1; } if( a == 3 ) { printf("Correct\n");// return 1; } else { printf("Wrong\n");//return 0; } }
cross posted in : http://cboard.cprogramming.com/newth...newthread&f=4#
<< moderator edit: fixed code tags -- and thanks for mentioning the cross-post >>
Hi there
..
i think i have read something like this here:
try this link
http://pweb.netcom.com/~tjensen/ptr/pointers.htm
..
i think i have read something like this here:
try this link
http://pweb.netcom.com/~tjensen/ptr/pointers.htm
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.
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.
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>
#include <stdlib.h>
#include <string.h>
int isaMUstring( const char *text )
{
return strlen(text) == strspn(text, "MIU");
}
int main()
{
int size;
char *input;
printf("Enter the size of string.");
scanf("%d", &size);
input = malloc(size + 1);
if ( input )
{
printf("Enter a character string(consist of 'M', 'I', 'U' only) :\n");
scanf("%s", input);
if ( isaMUstring( input ) )
{
puts("Correct");
}
else
{
puts("Wrong");
}
}
return 0;
} "One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
![]() |
Similar Threads
- dynamic memory allocation (C++)
- 3d to 1d Array Conversion (C++)
- why memory allocation (C)
- memory allocation (C)
- Dynamic memory allocation homework (C++)
- How to use alloc's memory allocation functions? (C)
Other Threads in the C Forum
- Previous Thread: 3D Graphic Artist Looking for Pointers
- Next Thread: second largest num
| Thread Tools | Search this Thread |
* adobe ansi api array asterisks binarysearch calculate centimeter char character cm convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile createprocess() csyntax directory feet fflush fgets file floatingpointvalidation fork frequency function getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hacking highest homework i/o inches infiniteloop interest intmain() kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives match meter microsoft mqqueue mysql number oddnumber odf open opendocumentformat openwebfoundation owf pattern pdf performance posix power probleminc program programming pyramidusingturboccodes read recv recvblocked repetition scanf scheduling segmentationfault send single socketprograming socketprogramming stack standard strchr string suggestions systemcall unix urboc user variable voidmain() wab whythiscodecausesegmentationfault win32api windows.h






