473,434 Members | 1,503 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

malloc & pointers.

36
what`s the error message bout ?

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. void main()
  3. {
  4.     int size1,size2;
  5.     printf("size of array_1 = ?\n");
  6.     scanf("%d",&size1);
  7.     printf("size of array_2 = ?\n");
  8.     scanf("%d",&size2);
  9.     int *ptr1=malloc(size1*sizeof(int)), *ptr2=mlloc(size2*sizeof(int));
  10. }
  11.  
Sep 6 '09 #1
7 4219
Tassos Souris
152 100+
What errors do you get?

To use malloc you need stdlib. So do:
Expand|Select|Wrap|Line Numbers
  1. #include <stdblih.h>
First do:
Expand|Select|Wrap|Line Numbers
  1. 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:
Expand|Select|Wrap|Line Numbers
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int main( void ){
  6.    int size1;
  7.    int size2;
  8.    int *ptr1;
  9.    int *ptr2;
  10.  
  11.    // Read the values correctly as you did
  12.  
  13.    // do malloc correctly
  14.   // some check here
  15.    assert( ptr1 != NULL );
  16.    assert( ptr2 != NULL );
  17.  
  18.    // Do work with ptr1 and ptr2
  19.  
  20.    // Do not forget this
  21.    free( ptr1 );
  22.    free( ptr2 );
  23.    exit( EXIT_SUCCESS );
  24. }
  25.  
Sep 6 '09 #2
weaknessforcats
9,208 Expert Mod 8TB
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.
Sep 6 '09 #3
sedaw
36
line 10 : error C2143: syntax error : missing ';' before 'type'
Sep 6 '09 #4
newb16
687 512MB
But there is no word 'type' in your code.
Sep 6 '09 #5
drhowarddrfine
7,435 Expert 4TB
Your second 'malloc' is spelled wrong. "mlloc"
Sep 6 '09 #6
sedaw
36
@newb16
type = int.


anyway

i think the syntax is wrong.

in this case theres no errors but warning message:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h> 
  2. void main() 
  3.     int size1,size2, *ptr1, *ptr2; 
  4.     printf("size of array_1 = ?\n"); 
  5.     scanf("%d",&size1); 
  6.     printf("size of array_2 = ?\n"); 
  7.     scanf("%d",&size2); 
  8.     ptr1=malloc(size1*sizeof(int)), 
  9.     ptr2=malloc(size2*sizeof(int)); 
  10.  

warning C4047: '=' : 'int *' differs in levels of indirection from 'int'
Sep 6 '09 #7
donbock
2,426 Expert 2GB
@sedaw
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.
Sep 7 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: john smith | last post by:
Can someone please explain to me what is happening when I do a malloc(0). This is what I did. int* p = (int*)malloc(0); Then I printed the value of p and of course it was non-null. But...
4
by: Cadderly | last post by:
Hi all, I have the following pseudo-c-code which parse a command line, and puts the command and its arguments in an array, and I can't figure out where/how I should use free: main(void):...
14
by: dam_fool_2003 | last post by:
Friends, cannot we malloc a array? So, I tried the following code: int main(void) { unsigned int y={1,3,6},i,j; for(i=0;i<3;i++) printf("before =%d\n",y); *y = 7; /* 1*/
7
by: Ramprasad A Padmanabhan | last post by:
Hello all, If I assign one pointer to another , I had assumed I can call them interchangably. I can explain by the example below. I am doing a malloc to char* a which is the same as char* b....
10
by: Ian Todd | last post by:
Hi, I am trying to read in a list of data from a file. Each line has a string in its first column. This is what i want to read. I could start by saying char to read in 1000 lines to the array( i...
7
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
7
by: Fatted | last post by:
I'm trying to learn how to create arrays dynamically. But its just not happening. Have a look at code below and point and laugh where appropriate... First part of program, I'm using an array of...
1
by: Kevin | last post by:
Hi all, I clearly have an issue with some pointers, structures, and memory allocation. Its probably pritty basic, but I'm a little stuck. Any help would be greatly appreciated. I'd like...
3
by: Petr Pavlu | last post by:
Hello, I have two questions how the functions should be written. I read the FAQ but didn't find any answer. If there is any please point me out. I. Cleanup code Consider I have to open file1,...
25
by: jbholman | last post by:
I am pretty new to C and doing my first project in C. I actually read almost the entire FAQ, but can't seem to figure out this problem. I have a structure. I have a list of these structures. ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.