944,123 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 5545
  • C RSS
Aug 31st, 2005
0

memory allocation ptr to array? how?

Expand Post »
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.



  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int isaMUstring( const char *string );
  6.  
  7.  
  8.  
  9.  
  10. int main(){
  11.  
  12. int size;
  13. char input[20];
  14.  
  15. char *ptr;
  16.  
  17.  
  18. printf("Enter the size of string.");
  19. scanf("%d",&size);
  20.  
  21. ptr = (char *)malloc((size +1) * sizeof(char *));
  22.  
  23.  
  24.  
  25. if( ptr) {
  26.  
  27. printf("Enter a character string(consist of 'M', 'I', 'U' only) :\n");
  28. scanf("%s",&input);
  29.  
  30.  
  31. isaMUstring( input );
  32.  
  33. }
  34.  
  35. return 0;
  36. }
  37.  
  38. int isaMUstring( const char *string ) {
  39.  
  40.  
  41. const char *cha1 = "M";
  42. const char *cha2 = "I";
  43. const char *cha3 = "U";
  44. int a;
  45.  
  46. a = 0;
  47.  
  48. if( strpbrk( string, cha1) != NULL) {
  49. a += 1;
  50. }
  51. if( strpbrk( string, cha2) != NULL) {
  52. a += 1;
  53. }
  54. if( strpbrk( string, cha3 ) != NULL) {
  55. a += 1;
  56. }
  57.  
  58. if( a == 3 ) {
  59.  
  60. printf("Correct\n");// return 1;
  61. }
  62. else {
  63. printf("Wrong\n");//return 0;
  64. }
  65.  
  66. }

cross posted in : http://cboard.cprogramming.com/newth...newthread&f=4#

<< moderator edit: fixed code tags -- and thanks for mentioning the cross-post >>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kokopo2 is offline Offline
13 posts
since Aug 2005
Aug 31st, 2005
0

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Enzo is offline Offline
4 posts
since Aug 2005
Aug 31st, 2005
0

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>
#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;
}
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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 31st, 2005
0

Re: memory allocation ptr to array? how?

Wouldn't it be safer to use fgets() rather then scanf()?
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: 3D Graphic Artist Looking for Pointers
Next Thread in C Forum Timeline: How to decrease the amount of memory that a process/executable consume?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC