Connecting Tech Pros Worldwide Help | Site Map

Malloc Problem

Member
 
Join Date: Apr 2007
Location: Malaysia
Posts: 55
#1: Apr 10 '07
Why this program have run time error ?

Expand|Select|Wrap|Line Numbers
  1. // Malloc.c  -- String version
  2.  
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5.  
  6. int main(int argc, char * argv[])
  7. {
  8.     int number, loop;
  9.     char *string;
  10.  
  11.     printf("How many number of characters : ");
  12.     scanf("%d", &number);
  13.  
  14.     string = malloc (number);
  15.  
  16.     for (loop=0;loop<number;loop++)
  17.     {
  18.         scanf("%c", &string[loop]);
  19.     }
  20.  
  21.     for (loop=0;loop<number;loop++)
  22.     {
  23.         printf("The string is %s", *(string + loop));
  24.     }
  25.  
  26.     return 0;
  27.  
  28. }
  29.  
  30.  
  31. // Why this program got run time error ? 
  32. // Please correct my mistake and i can learn from mistake
  33.  
  34.  



Thanks for oyur help.


Your help is greatly appreciated by me and others.
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,165
#2: Apr 10 '07

re: Malloc Problem


There are 2 errors.

1. You have not zero terminated your string. In C/C++ the end of character arrays used as strings are indicated by the 0 character '\0'. You have not put one in your variable string and you have not allocated memory for it (you need a + 1). An array of characters holding a string must always be at least 1 longer than the string itself to hold this 0 terminator.

2. In the code line
Expand|Select|Wrap|Line Numbers
  1.         printf("The string is %s", *(string + loop));
%s indicates a string, a variable of type char * but you pass it a variable of type char. Additionally by using (string + number) you actually reference the position where the 0 terminator for the string should be (but isn't).
Member
 
Join Date: Apr 2007
Location: Malaysia
Posts: 55
#3: Apr 11 '07

re: Malloc Problem


I couldn't understand what you say in second infromation. Sorry for my stupidness.

By the way, i have changed the size to store null terminated character but still have run time error.

Below is my code:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. // Malloc.c  -- String version
  4.  
  5. #include<stdio.h>
  6. #include<stdlib.h>
  7.  
  8. int main(int argc, char * argv[])
  9. {
  10.     int number, loop;
  11.     char *string;
  12.  
  13.     printf("How many number of characters : ");
  14.     scanf("%d", &number);
  15.  
  16.     string = malloc (sizeof(char) * (number + 1));
  17.  
  18.     for (loop=0;loop<number;loop++)
  19.     {
  20.         scanf("%c", &string[loop]);
  21.     }
  22.  
  23.     *(string + number) = '\0';
  24.     for (loop=0;loop<number;loop++)
  25.     {
  26.         printf("The string is %s", *(string + loop));
  27.     }
  28.  
  29.     return 0;
  30.  
  31. }
  32.  
  33.  
  34. // Why this program got run time error ? 
  35. // Please correct my mistake and i can learn from mistake
  36.  
  37.  
  38.  

Thanks for your help.

Your help is greatly appreciated by me and others.
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,165
#4: Apr 11 '07

re: Malloc Problem


Quote:

Originally Posted by Peterwkc

I couldn't understand what you say in second infromation. Sorry for my stupidness.

Well why don't you try explain to me what you think

*(string + loop)

in your printf line represents.
Ganon11's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Posts: 3,428
#5: Apr 11 '07

re: Malloc Problem


Also, when you assign string to the result of your malloc() call, you don't cast the result to a (char *). malloc() returns a void *, not a char*.
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,165
#6: Apr 11 '07

re: Malloc Problem


Quote:

Originally Posted by Ganon11

Also, when you assign string to the result of your malloc() call, you don't cast the result to a (char *). malloc() returns a void *, not a char*.

Strictly speaking that is not an error because this is C code not C++ code. C supplies an inctrinsic cast from void * to most other data pointer types which C++ does not because of its stricter typing. In fact in C it is an error to put in unnecessary casts like that amd to override the compilers intrisic casting rules.
Member
 
Join Date: Mar 2007
Posts: 51
#7: Apr 12 '07

re: Malloc Problem


Hi,
I think the problem is with the scanf statement inside the first for loop.

It is not advisable to use scanf for a character. For More details please look into this website.

http://www.gidnetwork.com/b-60.html

And in the printf statement inside the second for loop should use "%c" format.


Regards,
chella
Member
 
Join Date: Apr 2007
Location: Malaysia
Posts: 55
#8: Apr 22 '07

re: Malloc Problem


Thanks for all the reply.
Reply


Similar C / C++ bytes