Malloc Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Peterwkc
    New Member
    • Apr 2007
    • 55

    Malloc Problem

    Why this program have run time error ?

    Code:
    // Malloc.c  -- String version
    
    #include<stdio.h>
    #include<stdlib.h>
    
    int main(int argc, char * argv[])
    {
    	int number, loop;
    	char *string;
    
    	printf("How many number of characters : ");
    	scanf("%d", &number);
    
    	string = malloc (number);
    
    	for (loop=0;loop<number;loop++)
    	{
    		scanf("%c", &string[loop]);
    	}
    	
    	for (loop=0;loop<number;loop++)
    	{
    		printf("The string is %s", *(string + loop));
    	}
    
    	return 0;
    
    }
    
    
    // Why this program got run time error ? 
    // Please correct my mistake and i can learn from mistake



    Thanks for oyur help.


    Your help is greatly appreciated by me and others.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    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
    Code:
    		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).

    Comment

    • Peterwkc
      New Member
      • Apr 2007
      • 55

      #3
      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:

      Code:
      
      // Malloc.c  -- String version
      
      #include<stdio.h>
      #include<stdlib.h>
      
      int main(int argc, char * argv[])
      {
      	int number, loop;
      	char *string;
      
      	printf("How many number of characters : ");
      	scanf("%d", &number);
      
      	string = malloc (sizeof(char) * (number + 1));
      
      	for (loop=0;loop<number;loop++)
      	{
      		scanf("%c", &string[loop]);
      	}
      	
      	*(string + number) = '\0';
      	for (loop=0;loop<number;loop++)
      	{
      		printf("The string is %s", *(string + loop));
      	}
      
      	return 0;
      
      }
      
      
      // Why this program got run time error ? 
      // Please correct my mistake and i can learn from mistake

      Thanks for your help.

      Your help is greatly appreciated by me and others.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        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.

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          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*.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            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.

            Comment

            • chella
              New Member
              • Mar 2007
              • 51

              #7
              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.



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


              Regards,
              chella

              Comment

              • Peterwkc
                New Member
                • Apr 2007
                • 55

                #8
                Thanks for all the reply.

                Comment

                Working...