memory allocate pointer to array ???

Discussion of programming on Linux, including shell scripting, perl, python, c/c++, mono, java. Whatever tickles your fancy.
Post Reply
kokopo2
Cadet
Posts: 1
Joined: Wed Aug 31, 2005 3:50 pm

memory allocate pointer to array ???

Post by kokopo2 »

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.

Code: Select all


#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");
	}
	else {
		printf("Wrong\n");
	}

}


cross posted:
http://cboard.cprogramming.com/showthread.php?t=69168

http://forums.devshed.com/showthread.ph ... ost1238592
soni
Naik
Posts: 70
Joined: Sat Oct 04, 2003 1:44 pm
Location: Karachi
Contact:

Post by soni »

hey

Code: Select all

char *ptr;
 ptr = (char *)malloc((size +1) * sizeof(char *));
 
this is not an array of charaters but a array in which the size of each element is equal to the size of near pointers.
How you will access the elements of the array ptr.
In your declaration char *ptr you say that it is an array of characters. But in your definition (when you allocate the memory) you make each element of the array equal to(say) 4 bytes instead of usual one byte.
So when you traverse through the array like ptr[0], ptr[1] to ptr[3] you according to the declaration went through 4 different elements but according to definition you had gone
through the single first(0th) element, piece by piece. I hope you are getting my point.

Code: Select all


//this is your definition, when allocate the memory

[b]float num;[b] 

//this is how you accessed it in your programm.

char* ptr;
ptr = (char *)&num;
printf("%c..%c ",  ptr[0] to ptr[3] ....
         


Now to the other problem try this ....

Code: Select all


int isaMUstring( const char *string ) {
   if(strcmp(string,"MIU") == 0)
     return 1;
   else
     return 0;
}

Please do not open M$Windoze.
outhanger
Battalion Quarter Master Havaldaar
Posts: 214
Joined: Sun Apr 18, 2004 5:58 pm
Location: CEME, Pindi

Post by outhanger »

This is how you would normally store a function's return value:

Code: Select all

int main()
{
int retval;
    .
    .
    .
retval = isaMUstring(parameter1, ...) // where isaMUstring returns an int of course
    .
    .
}
I think your main() should look like this. I don't store the function's return value in a variable in the following:

Code: Select all

int main()
{
char *ptr;
int num;
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",ptr);

   if (isaMUstring(ptr)) // assuming function returns non-zero value on success
     puts("Correct");
   else
      puts("Wrong");
   }

   return 0;
}
Your isaMUstring() might look like this. I check each character, one by one, if it is either one of 'M', 'I' or 'U':

Code: Select all

int isaMUstring(char *string)
{
  int i=0;
  for (i=0; i<strlen(string); i++)
    {
      if(string[i]=='M' || string[i]=='I' || string[i]=='U')
        continue;
      else return 0; // if a character is neither of the above three, function exits with 0
    }
  return 1;
}
Hope this helps. All of the above code is unverified so simply pasting it and expecting it to work might disappointing. Please try to understand the logic behind the code.
BTW, I don't understand what soni is trying to make you do.
"Where blasphemy is a crime, religious freedom does not exist." - Stallman to Musharraf
Post Reply