Solved

Simple C Code,

Posted on 2004-08-26
6
284 Views
Last Modified: 2010-04-15
hi experts,

I wrote a code in C as shown below and its giving the following error.
Cannot convert int* to int
Type mismatch in parameter1 in call to Junk(int,int)
Cannot convert int* to int
Type mismatch in parameter2 in call to Junk(int,int)

could anyone explain to me why this error? pls explain in a simple way, newbie here>)
#include<stdio.h>
#include<conio.h>
void junk(int,int);
void main()
{
int i=5,j=2;
junk(&i,&j);
printf ("\n%d%d",i,j);
}
void junk(int *i,int *j)
{

*i=*i**i;
*j=*j**j;
}
really appreciate the help
thanks in advance
deep
0
Comment
Question by:deepthiji
  • 4
  • 2
6 Comments
 
LVL 11

Expert Comment

by:avizit
ID: 11908745
in your functrion prototype you have


void junk(int,int);

while for your function definition you have


void junk(int *i,int *j)
{

*i=*i**i;
*j=*j**j;
}


so t hey dont match



0
 
LVL 11

Expert Comment

by:avizit
ID: 11908749
so you can just change the funtion prototype

from

void junk(int,int);

to

void junk(int *,int *);



0
 
LVL 3

Author Comment

by:deepthiji
ID: 11908761
sorrry i did not get that, As i said i just started learnning C,
>>in your functrion prototype you have
>>void junk(int,int);

>>while for your function definition you have
>>void junk(int *i,int *j)

what is wrong, should i declare void junk(int*,int*) at the bginning?
please explain some more,


0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 11

Expert Comment

by:avizit
ID: 11908790
right ..


in the third line of your code it is the function prototype , here you tell the compiler what yuor function looks like i.,e what is its return type and what are its arguments

so you declare the function as

void junk(int,int);   i.e you say that the function named 'junk' retunrs no value and takes two integers as arguments.

but when you actuallky define the function you are defining it as

void junk(int *i,int *j)
{

*i=*i**i;
*j=*j**j;
}

i.e here you say that the function  returns  no value ( i.e void) and takes two pointer to integers (int *i, int *j)   as arguments

so you see your declaration and definition has a mismatch

thats why the error


>>>>
Cannot convert int* to int
Type mismatch in parameter1 in call to Junk(int,int)
Cannot convert int* to int
Type mismatch in parameter2 in call to Junk(int,int)
<<<<<


hence to correct it you should change the prototype to match the fucntion definition
so yuo can chnage the third line of your program

from

void junk(int,int);

to

void junk(int *,int *);




0
 
LVL 11

Accepted Solution

by:
avizit earned 500 total points
ID: 11908810
also your expressions look messy ,
it is advisable to use brackets  to make the meaning more clear

change

*i=*i**i;
*j=*j**j;

to

*i  =  (*i)  *  (*i);
*j  =  (*j)  *  (*j);

This way its more readable and easier to get the meaning at a glance
0
 
LVL 3

Author Comment

by:deepthiji
ID: 11908816
Got it, thanks a loot for the help,
thanks once again.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question