473,385 Members | 1,855 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,385 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 4217
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. ...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.