malloc & pointers.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sedaw
    New Member
    • Jan 2009
    • 36

    malloc & pointers.

    what`s the error message bout ?

    Code:
    #include <stdio.h>
    void main()
    {
    	int size1,size2;
    	printf("size of array_1 = ?\n");
    	scanf("%d",&size1);
    	printf("size of array_2 = ?\n");
    	scanf("%d",&size2);
    	int *ptr1=malloc(size1*sizeof(int)), *ptr2=mlloc(size2*sizeof(int));
    }
  • Tassos Souris
    New Member
    • Aug 2008
    • 152

    #2
    What errors do you get?

    To use malloc you need stdlib. So do:
    Code:
    #include <stdblih.h>
    First do:
    Code:
    int main( void )
    and note that if you are not using a C99 compliant compiler then you cannot mix declarations with statements so you must declare ptr1 and ptr2 at the top.

    How this code might be made is like this:
    Code:
    #include <assert.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main( void ){
       int size1;
       int size2;
       int *ptr1;
       int *ptr2;
    
       // Read the values correctly as you did
     
       // do malloc correctly
      // some check here
       assert( ptr1 != NULL );
       assert( ptr2 != NULL );
    
       // Do work with ptr1 and ptr2
    
       // Do not forget this
       free( ptr1 );
       free( ptr2 );
       exit( EXIT_SUCCESS );
    }
    Last edited by Tassos Souris; Sep 6 '09, 06:13 PM. Reason: Been a victim of using tab....

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      1) main() returns an int.
      2) If this is C all variables must be declared at the top of the function.
      3) These are guesses since you don't say what the error is.

      Comment

      • sedaw
        New Member
        • Jan 2009
        • 36

        #4
        line 10 : error C2143: syntax error : missing ';' before 'type'

        Comment

        • newb16
          Contributor
          • Jul 2008
          • 687

          #5
          But there is no word 'type' in your code.

          Comment

          • drhowarddrfine
            Recognized Expert Expert
            • Sep 2006
            • 7434

            #6
            Your second 'malloc' is spelled wrong. "mlloc"

            Comment

            • sedaw
              New Member
              • Jan 2009
              • 36

              #7
              Originally posted by newb16
              But there is no word 'type' in your code.
              type = int.


              anyway

              i think the syntax is wrong.

              in this case theres no errors but warning message:

              Code:
              #include <stdio.h> 
              void main() 
              { 
                  int size1,size2, *ptr1, *ptr2; 
                  printf("size of array_1 = ?\n"); 
                  scanf("%d",&size1); 
                  printf("size of array_2 = ?\n"); 
                  scanf("%d",&size2); 
                  ptr1=malloc(size1*sizeof(int)), 
                  ptr2=malloc(size2*sizeof(int)); 
              }

              warning C4047: '=' : 'int *' differs in levels of indirection from 'int'

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #8
                Originally posted by sedaw
                warning C4047: '=' : 'int *' differs in levels of indirection from 'int'
                Header stdio.h provides the prototype for scanf, but not the prototype for malloc. Lacking a prototype, the default is for the compiler to assume the function returns an int. The warning alerts you that you are assigning this presumed int return value from malloc to an int* variable. Tassos told you the name of the header that declares malloc in reply #2.

                By the way, function main is different from all other functions in Standard C. The prototype for this function is implicit in the compiler. And the implicit prototype declares this function to be returning an int. By defining main to return void you are contradicting the implicit prototype. Most compilers will let you get away with this; but there are compilers out there where doing so will provoke either a compile-time error or a run-time error. It is best if you get in the habit of writing fully portable code. If you change the definition of main then don't forget to add a return statement. This C FAQ explains the issue further.

                Comment

                Working...