I m learning c (programming in ANSI c -> fifth edition) and facing the below issue:
I'm implementing one program with pointers to functions
#include<stdio.h>
//void swap (int *a, int *b); //function declaration
int main()
{
int m = 25;
int n = 100;
printf("m is %d, n is %d\n", m, n);
swap(&m, &n); //calling a function
printf("m is %d, n is %d\n", m, n);
return 0;
}
void swap (int *a, int *b) //function implementation
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
in the above program comment the line void swap (int *a, int *b); then program run file but give one suggestion here facing the issue why here give the suggestion
main.c:10:3: warning: implicit declaration of function ‘swap’ [-Wimplicit-function-declaration]
main.c:16:6: warning: conflicting types for ‘swap’
main.c:10:3: note: previous implicit declaration of ‘swap’ was here
m is 25, n is 100
m is 100, n is 25
when I m uncomment this line void swap (int *a, int *b); then program run fine
#include<stdio.h>
void swap (int *a, int *b); //function declaration
int main()
{
int m = 25;
int n = 100;
printf("m is %d, n is %d\n", m, n);
swap(&m, &n); //calling a function
printf("m is %d, n is %d\n", m, n);
return 0;
}
void swap (int *a, int *b) //function implementation
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
problem is why not allowed this one in the above program
void swap (int, int); //function declaration
and give the error
error:
conflicting types for ‘swap’
void swap (int *a, int *b) //function implementation
but in book example allowed
void swap (int *a, int *b);
This here before main() is called function signature that should match function declaration
below.
void swap (int *a, int *b){
...
}
But the same function signature can be written as
void swap (int *, int *);
And not
void swap (int , int ); //This will result in type mismatch
I guess this is what you are asking.
NOTE: Forward declaration is important here which lets you declare first and define later helps maintain code readability.
Related
I have some work with this code and i need a swap function that can get a arrays like a[j].
How I need to transport to another function something like this?
#include <stdio.h>
void bubble_sort(int *a, int n) {
int i, j;
for (i = 0; i < n; i++) {
for (j = 0; j < n - i - 1; j++) {
if (a[j] > a[j + 1]) swap(&a[j], &a[j + 1]);
}
}
}
This is the code, so how I can call swap function with a[j]? Do I need to call function like this or?
int swap (int a[],int b[])
int swap (int *a,int *b)
With this second call i am sure that it will work Am i right ? But how can i call this function like the first example?
#define MAX 100
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
return 0;
}
void bubble_sort(int a[], int n) {
int i;
for(i=0;i<n;i++)
{
if(a[i]<a[i+1])
swap(a[i],a[i+1]);
}
return 0;
}
int main()
{
int a[4]={1,2,3,0};
int n=4;
bubble_sort(a,n);
for(int i=0;i<n;i++)
{
printf("%d",a[i]);
}
}
I used that code Segmentation fault (core dumped)
Your function needs to take 2 pointers like this:
void swap(int * const a, int * const b) {
int temp = *a;
*a = *b;
*b = temp;
}
int swap (int a[], int b[]); will also work since in function parameters int* a and int a[] are the same thing. But it is confusing - it implies that the pointer is pointing to an array, while you only want to swap 2 integers.
Here's how you could do it the other way:
void swap(int a[], int b[]) {
int temp = *a;
*a = *b;
*b = temp;
}
I'm trying to create a function that accepts an array and two integers to manipulate and return in a struct.
What i have looks like this:
#include <stdio.h>
struct Results {
int *A; // Pointer para o Array
int N; // Comprimento do Array
};
int k, n;
struct Results solution(int A[], int N, int K);
int main(void){
int a[] = {1,2,3};
struct Results out;
k = 1;
n = sizeof(a)/sizeof(a[0]);
printf("n = %d \n", n);
out = solution(int a[], int n, int k);
// EXPECTED EXPRESSION !!
}
struct Results solution(int A[], int N, int K) {
struct Results outp;
outp.A = A;
outp.N = N;
return outp;
};
I can't pass from this point, the compiler tells me that a expression is expected when I declare the function.
I think this might be a basic syntax error...
You are mixing the syntax for defining or declaring a function with the actual call to that function:
out = solution(a, n, k);
The types of the parameters are only present in the prototype, not in the call.
I am learning C as a part of my curriculum, and I am still learning the concepts of pointers. The following example gives unpredicted results for me.
#include <stdio.h>
void Calculate(int *x, int *y, int *k, int *m) {
k = *x * *y;
m = *x + *y;
}
int main() {
int *k;
int *m;
int g = 10;
int h = 11;
Calculate(&g, &h, k, m);
printf("%d\n", m);
printf("%d", k);
}
Each execution is giving me different results, like
-1155180448
0
or
253276384
0
or
591649904
0
but if I change the code to
#include <stdio.h>
void Calculate(int *x, int *y, int *k, int *m) {
*k = *x * *y;
*m = *x + *y;
}
int main() {
int k;
int m;
int g = 10;
int h = 11;
Calculate(&g, &h, &k, &m);
printf("%d\n", m);
printf("%d", k);
}
it prints the correct values like
21
110
Isn't using int *k; int *m; and passing it to a function as somefunc(k, m) the same as int k; int m; somefunc(&k, &m)?
Kindly explain what is wrong here.
Thank you.
The function should be:
void Calculate(int *x, int *y, int *k, int *m) {
*k = *x * *y;
*m = *x + *y;
}
*k means an int stored in memory being pointed to by k. You don't want to modify k, you want to modify what is stored in the space k points to.
Then you call like this:
int a, b;
int g = 10;
int h = 11;
Calculate(&g, &h, &a, &b);
You tell the Calculate function whereabouts in memory the variables a and b are which will hold the result of the calculation.
Note: I used different variable names because it is confusing to use the same name k for an int in one place, and k for an int * in another place. Another plan of course would be to use k,m in main and use pk or pm in the function (meaning "pointer to k", etc.)
Also you don't need to use pointers to pass g and h and in fact this is a bad idea, unless you are planning to change those in the function too.
If you really want to test this call Calculate(&g, &h, k, m); as in your first example, passing integers g and h and integer pointers *k and *m, first make sure that the pointers really point at something, so they can be treated likewise as addresses of integer variables by your Calculate() function, or whichever function of syntax alike.
#include <stdio.h>
#include <stdlib.h> //add this header for malloc
void Calculate(int *x, int *y, int *k, int *m) {
*k = *x * *y;
*m = *x + *y;
}
int main()
{
int *k = malloc(sizeof(int)); //initialize the pointer to point at something
int *m = malloc(sizeof(int)); //initialize the pointer to point at something
int g = 10;
int h = 11;
Calculate(&g, &h, k, m);
printf("%d\n", *m);
printf("%d\n", *k);
free(k); //free the space pointed to by pointers!
free(m);
return 0;
}
This is not a very practical solution (at least as your example is concerned), but is here only to show how you should treat your pointers as function arguments from within your caller function.
I have to write a swap function for my bubble sort
That's what I've got:
void swap(int arr[], int size, int i, int j)
{
int temp = *(arr+i);
*(arr + i) = *(arr+j);
*(arr+j) = temp;
}
When I'm trying to run I get the following errors:
warning C4013: 'swap' undefined; assuming extern returning int
error C2371: 'swap' : redefinition; different basic types
When I do change the function to the type of int, it does work, any idea why?
I don't need a prototype because it's before the main function... do I?
Here's the whole code:
//BubbleSort
void bubbleSort(int arr[], int size)
{
int i,j;
for(i=0; i < size; i++)
{
for(j=i+1; j < size; j++)
{
if(*(arr+i) > *(arr+j))
{
/*temp = *(arr+i);
*(arr + i) = *(arr + j);
*(arr + j) = temp;*/
swap(arr,i,j);
}
}
}
}
void swap(int arr[], int i, int j)
{
int temp = *(arr+i);
*(arr + i) = *(arr+j);
*(arr+j) = temp;
}
void main()
{
int i, arr[] = {8,0,6,-22,9};
bubbleSort(arr, sizeof(arr)/sizeof(int));
for(i=0; i < sizeof(arr)/sizeof(int); i++)
{
printf("%d, ",*(arr+i));
}
printf("\n");
}
You seem to lack a proper prototype for the function.
Add
void swap(int arr[], int size, int i, int j);
before the first call.
Also, there's really little point in using such pointer-centric notation for the indexing, especially confusing since you declared the arr argument as an array. It's cleaner to just use:
const int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
Notice use of const for the temp value too, since it's not going to change after being assigned. Not a big deal in a 3-line function, but a good habit.
You need place void swap(int arr[], int i, int j) on top of void bubbleSort() because you're using swap() inside bubbleSort().
If not you will run into the implicit declaration of C, that is, in main(), you're calling bubbleSort(), and bubbleSort() will call swap(), but at this point, bubbleSort() does not know about you declaration of swap() since it is declared below it. So, what your compiler understand is that you're calling a swap() which is implicitly declared.
And later, when your compiler comes across your real declaration of void swap(int arr[], int i, int j), it will complain that it is a redefinition.
Other than moving your swap() declaration on to the top most, you can also solve by making the function declaration at the top most, and the definition below separately.
Besides, you don't seem to use pointer the right way, because you already passed int arr[] into the swap() function, where you can do direct swapping as pointed out by #unwind:
const int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
Consider this swap():
void swap(int *x, int *y){
int temp = *x;
*x=*y;
*y=temp;
}
This is important for you to learn that you can actually change the content of a variable by passing the address into a function, like swap(int *x, int *y), it will be very convenient. Example:
int x,y;
x=1;
y=1;
increment_both_coordinate(x,y);
//after this call, you want to have x = 2, y = 2
This can only be achieve using the method similar to swap(int *x, int *y). This is just an illustration, you will understand how useful this can be when you see them in future.
Inside bubbleSort() you call a function named swap() but, at that point in the code, there is no function named swap() either defined or declared.
Solution 1: move the defintion of swap() to before the definition of bubbleSort()
Solution 2: specify the prototype of swap() before defining bubbleSort()
If the function is void, you can't return a number.
Change your
return 0;
to
return;
Because you're returning 0. Take out the return statement and you should be fine, especially since you're operating on pointers instead of copy values.
Using "void" means the function doesn't return any value,but actually your function return "0",which is an int type.So you should use int instead of void before function definition.
Here is a function for swapping integers:
void swap(int *x, int *y) {
*x = *x ^ *y;
*y = *x ^ *y;
*x = *x ^ *y;
}
int main(int argc, char* argv) {
int a,b;
a = 5;
b = 10;
swap(&a, &b);
printf("a = %d, b = %d\n", a, b);
return 0;
}
You can swap two array cells this way: swap(&arr[i], &arr[j]);
This code is working but its not exactly what I want. Is anyone have any idea how to make it correct and without q sort?. The idea is to understand how to use pointers.
The three numbers should be random between -3 and 12. The code below is something similar, and the closest I have found. Any help would be much appreciated. Thanks in advance!!.
#include <stdio.h>
#include <stdlib.h>
//functions
int compare(const void *a, const void *b)
{
const int *ia = a;
const int *ib = b;
if (*ia < *ib)
return -1;
else if (*ia > *ib)
return +1;
return 0;
}
//qsort function
void sort3(int *a, int *b, int *c)
{
int temp[3];
temp[0] = *a;
temp[1] = *b;
temp[2] = *c;
qsort(temp, 3, sizeof(int), &compare);
*a = temp[0];
*b = temp[1];
*c = temp[2];
}
//random function
int rand_int(int a, int b)
{
return rand()%(b-a+1)+a;
}
int main(void)
{
//declaration of variables
int a,b,c;
int rand_int(int a, int b);
srand(time(0));
a = rand_int(-3,12);
b = rand_int(-3,12);
c = rand_int(-3,12);
printf("%i %i %i\n", a, b, c);
sort3(&a, &b, &c);
printf("%i %i %i\n", a, b, c);
return 0;
}
You don't need the compare() function if you don't want to use qsort().
You can rewrite sort3() like this:
void compare_and_swap(int *a, int *b) {
int t;
if (*a > *b) {
t = *a;
*a = *b;
*b = t;
}
}
void sort3(int *a, int *b, int *c) {
compare_and_swap(a, b);
compare_and_swap(a, c);
compare_and_swap(b, c);
}
This is actually a "bubble sort".
This is a lot of trouble to go for to sort 3 integers. Use if statements.
If the goal is actually to understand pointers, they seem intimidating but they're not so bad. Basically, they're a number that happens to be an address. You can manipulate like them numbers, but if you dereference them (with *), you can get the value there. This cuts both ways, though, because there's not much stopping you from dereferencing a value - which probably crashes your program (or more scarily, maybe not).
As long as you keep in mind what's an address and what's a value, you should be OK.