Solved

Simple C Code,

Posted on 2004-08-26
6
282 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops 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.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now