LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-13-2013, 10:23 AM   #1
ofbodur
LQ Newbie
 
Registered: May 2013
Location: Istanbul/Turkey
Distribution: Mageia 2
Posts: 5

Rep: Reputation: Disabled
use of swap with pointers gave error.


Dear visitors,

I typed the code below to my text editor and executed by using gcc compiler:
Code:
#include<stdio.h>
main()
{     int a,b,c;
      scanf(“%d*“,&a);
      scanf(“*%d*“,&b);
      printf(“%d %d \n“,a,b);
       swap(&a, &b);
       printf(“%d %d \n”,a,b);
}
/*This is the C subroutine that does the swap using pointers.*/
void swap(int *px, int *py)  
/* interchange *px and *py */
{  int temp;
temp = *px;
*px = *py;
*py = temp;   
}
but, it gave me the error below:

new.c:11:6: warning: conflicting types for ‘swap’ [enabled by default]
new.c:7:8: note: previous implicit declaration of ‘swap’ was here



what is the problem here?
Any further help is muchly appreciated..

Last edited by ofbodur; 05-13-2013 at 10:26 AM.
 
Old 05-13-2013, 11:26 AM   #2
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,044

Rep: Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100
You need to declare the function before calling it.
Code:
#include<stdio.h>
void swap(int *px, int *py);
main()
{     int a,b,c;
      scanf(“%d*“,&a);
      scanf(“*%d*“,&b);
      printf(“%d %d \n“,a,b);
       swap(&a, &b);
       printf(“%d %d \n”,a,b);
}
/*This is the C subroutine that does the swap using pointers.*/
void swap(int *px, int *py)  
/* interchange *px and *py */
{  int temp;
temp = *px;
*px = *py;
*py = temp;   
}

Last edited by johnsfine; 05-13-2013 at 11:29 AM.
 
1 members found this post helpful.
Old 05-13-2013, 11:39 AM   #3
ofbodur
LQ Newbie
 
Registered: May 2013
Location: Istanbul/Turkey
Distribution: Mageia 2
Posts: 5

Original Poster
Rep: Reputation: Disabled
is it also OK to declare void swap(); in the main function?
 
Old 05-13-2013, 10:56 PM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,571

Rep: Reputation: 471Reputation: 471Reputation: 471Reputation: 471Reputation: 471
possible, but not recommended:

Code:
int main (void)
{
    void swap (int *, int *);
...

Last edited by NevemTeve; 05-13-2013 at 10:58 PM.
 
Old 05-16-2013, 10:11 AM   #5
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,230

Rep: Reputation: 1070Reputation: 1070Reputation: 1070Reputation: 1070Reputation: 1070Reputation: 1070Reputation: 1070Reputation: 1070
Agreed. The "C" language allows such declarations to be made in a per-function limited scope, but that's not normally what is done. The normal practice in that language is to issue declarations in advance, so that they have a per-file-level scope. (Case in point is stdio.h itself, which, if you care to open the file and look at it, is chock-full of just such declarations, concerning the standard-I/O library.) The preferred practice, "when in Rome," is "to do what the Romans do."

Last edited by sundialsvcs; 05-16-2013 at 10:12 AM.
 
1 members found this post helpful.
  


Reply

Tags
swap


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error `gave up waiting for root device`/ can't boot kernel Panceisto Linux - Newbie 5 05-25-2010 01:22 PM
Write-error on swap-device error on package install slack 12.2 chickenlinux Slackware 11 01-25-2009 08:14 PM
swap file instead of swap disk "dd" error rezwits Linux - Newbie 5 12-16-2007 12:34 AM
C++ template smart pointers for class hiearchies in a vector push_back error R00ts Programming 7 09-20-2004 01:14 PM
error code for pointers and 2-Dimensional arrays shams Programming 1 08-06-2004 10:00 PM


All times are GMT -5. The time now is 09:27 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration