C Error - Expected declaration specifiers or '...' before '*' token - c

I'm trying to get my head around pointers and I'm trying to write a program which will swap two numbers using pointers. However, I'm getting the error as stated in the title. Here is my code:
//This program swaps two numbers using pointers
#include <stdio.h>
void swap(*val1, *val2);
int main() {
int num1, num2;
int *pNum1, *pNum2;
printf("Enter number 1:\n");
scanf("%d", &num1);
printf("Enter number 2:\n");
scanf("%d", &num2);
pNum1 = &num1;
pNum2 = &num2;
printf("Numbers not swapped: %d, %d\n", *pNum1, *pNum2);
swap(pNum1, pNum2);
return 0;
}
void swap(*val1, *val2) {
int temp;
temp = val1;
val1 = val2;
val2 = temp;
printf("Numbers swapped: %d, %d\n", *val1, *val2);
return;
}

void swap(*val1, *val2);
should be
void swap(int *val1, int *val2);
You should then pass
swap(&num1,&num2);
If you pass a pointer then you are passing a copy of it.You need to pass the address. No need of having pointers in the calling function you can directly pass the address of the variables.
void swap(int *p,int *q)
{
int t = *p;
*p = *q;
*q = t;
}

Related

How to compare 2 pointers variable in C?

My function is unable to compare 2 pointer values. The main function has two variables a and b. Swap function swaps the value of a and b. In my max function I cant find the bigger number between c and a. It is not working properly as a always become the bigger number.
//swap two varibales
int swap(int *a, int *b )
{
int temp = *a;
*a= *b;
*b = temp;
printf("After swap: A = %d and B = %d", *a, *b);
}
This function takes the input of c and adds all three numbers.
int additon(int *a, int *b, int *c, int *total)
{
printf(" \nInput Numbers you want: ");
scanf("%d", c);
*total = *a + *b + *c;
printf("\n\nTotal value of numbers %d, %d, %d are = %d",*a,*b, *c, *total );
}
Function which compares between a and c [problem] :
int max(int *a, int *b, int *c,int *maxNum)
{
if( a > c)
{
printf("\n\n A = %d is bigger than C = %d", *a,*c);
}
else if( a < c)
{
printf("\n\n A = %d is smaller than C = %d",*a,*c);
}
}
Main function:
int main(int *total, int *maxNum)
{
int *a = 10;
int *b = 20;
int *c;
printf("Before swap: A = %d and B = %d\n", a,b);
swap(&a, &b);
additon(&a, &b, &c, &total);
max(&a, &b, &c, &maxNum);
return 0;
}
You need to dereference the pointers. Like this:
if( *a > *c)
{
printf("\n\n A = %d is bigger than C = %d", *a,*c);
}
else if( *a < *c)
{
printf("\n\n A = %d is smaller than C = %d",*a,*c);
}
In the previous version your were comparing the addresses the pointers held.

qsort for structures

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct Student
{
char nume[20];
int grupa,nr_credite;
} S;
int cmpg(const void *a, const void *b);
int cmpc(const void *a, const void *b);
void ex();
int main()
{
ex(); //main program
return(0);
}
void ex() //sorting function
{
int n,i,c;
S st[100];
scanf("%d", &n);
for(i=0; i<n; i++)
scanf("%s %d %d", st[i].nume, &st[i].grupa, &st[i].nr_credite);
size_t no = sizeof(S)/sizeof(st->grupa);
size_t noo = sizeof(S)/sizeof(st->nr_credite);
qsort(st->grupa, no, sizeof(S), cmpg);
qsort(st->grupa, noo, sizeof(S), cmpc);
for(i=0; i<n; i++)
printf("%s %d %d", st[i].nume, &st[i].grupa, &st[i].nr_credite);
}
int cmpg(const void *a, const void *b)
{
struct Student *ia = (struct Student *)a;
struct Student *ib = (struct Student *)b;
return (int)(ia->grupa - ib->grupa);
}
int cmpc(const void *a, const void *b)
{
struct Student *ia = (struct Student *)a;
struct Student *ib = (struct Student *)b;
return (int)(ib->nr_credite - ia->nr_credite);
}
So the thing is I have some students in some different groups and they have a different number of credits. I want to use qsort to order them by group (ascending) and inside of each group to sort them by the number of credits(descending).
I have this code but it is stopping with this exit code: Process terminated with status -1073741819 (0 minute(s), 11 second(s)).
The problem comes from your calls of qsort. What qsort expects is the array to sort, the number of elements that should be sorted, the size of each element and the comparison function. So what you need is:
qsort(st, n, sizeof(S), cmpg);
qsort(st, n, sizeof(S), cmpc);
And your printf is also wrong:
for(i=0; i<n; i++)
printf("%s %d %d", st[i].nume, st[i].grupa, st[i].nr_credite);
BTW, you never test for errors which is bad:
if n>100, you will get an out of bond access on S
if there is an incorrect value in one the the answers, you will have read errors but still proceed with indeterminate values.
As noted in comments, this only fixes the error. But as qsort is not required to be a stable sort, two consecutive sorts may not produce what you want.
You want this:
void ex()
{
int n, i;
S st[100];
scanf("%d", &n);
for (i = 0; i<n; i++)
scanf("%s %d %d", st[i].nume, &st[i].grupa, &st[i].nr_credite);
qsort(st, n, sizeof(S), cmpg);
// ^ ^ ^
// | | |
// | | size of one element
// | |-- number of elements in array
// |-- address of first element of the array
//
qsort(st, n, sizeof(S), cmpc);
for (i = 0; i<n; i++)
printf("%s %d %d\n", st[i].nume, st[i].grupa, st[i].nr_credite); // removed &s
}

How you make procedure in C?

So I'm new to C, I'm trying to make procedure to swap value of 2 variables.
When I run this the swap didn't work.
#include <stdio.h>
#include <stdlib.h>
void swap(int A,int B)
{
A = A + B;
B = A - B;
A = A - B;
}
int main(void){
int num1,num2;
printf("insert first number :\n");
scanf("%d",&num1);
printf("insert second number :\n");
scanf("%d",&num2);
swap(num1,num2);
printf("%d %d\n",num1,num2);
return 0;
}
That is because arguments are passed by value and modifying them in callee won't affect caller's local variables. Use pointers to modify ones.
#include <stdio.h>
#include <stdlib.h>
void swap(int* A,int* B)
{
*A = *A + *B;
*B = *A - *B;
*A = *A - *B;
}
int main(void){
int num1=0,num2=0; /* initialize for in case scanf() fails */
printf("insert first number :\n");
scanf("%d",&num1);
printf("insert second number :\n");
scanf("%d",&num2);
swap(&num1,&num2);
printf("%d %d\n",num1,num2);
return 0;
}
It didn't work because when you call the procedure, it is made a copy of the arguments to the new scope.
The right way is with the address of the variables.
Change to:
swap(&num1,&num2);
and
void swap(int *A,int *B)
{
*A = *A + *B;
*B = *A - *B;
*A = *A - *B;
}

C Program, function sorting through pointers

This program is supposed to take an array, and sort it from lowest to highest value. My program won't sort any values though. I believe the error is in the selectionSort. The values i and j are present in the function, I printed them out inside the function but they are not passed into the swap function. I tried making i and j pointers but it didn't work. I just have no clue on what to do next. Any help would be appreciated.
#include <stdio.h>
#define N 5
void selectionSort(int *a, int n);
int *findLargest(int *a, int n);
void swap(int *p, int *q);
int main(void)
{
int i;
int a[N];
printf("Enter %d numbers: ", N);
for (i = 0; i < N; i++) {
scanf("%d", &a[i]);
}
selectionSort(a, N);
printf("In sorted order:");
for (i = 0; i < N; i++) {
printf(" %d", a[i]);
}
printf("\n");
return 0;
}
void selectionSort(int *a, int n)
{
int *p = a;
int i;
int j;
if (n == 1) {
return;
}
i = *(p+n-1);
j = *findLargest(a, n);
swap(&i, &j);
selectionSort(a, n - 1);
}
int *findLargest(int *a, int n)
{
int *p;
int *p_max = a;
for(p = a + 1; p < a + n - 1; p++) {
if ( *p > *p_max)
p_max = p;
}
return p_max;
}
void swap(int *p, int *q)
{
int temp = *(p-1);
*(p-1) = *q;
*q = temp;
}
The problem is in your call of swap: you swap the content of two local variables
int i;
int j;
... // Some other code, then
swap(&i, &j);
This has no effect on the original array. You should be passing p+n-1 and findLargest(a, n) directly, or store their results in pointers, not in ints:
swap(p+n-1, findLargest(a, n));
In addition, your swap is broken: rather than swapping the content of two pointers, it assumes that p points one element past the target location. This is a bad assumption to make in a general-purpose function, such as swap, and it also leads to undefined behavior in your program.
void swap(int *p, int *q) {
int temp = *p;
*p = *q;
*q = temp;
}

how to get the correct input value from the pointer [duplicate]

This question already has answers here:
How to find the size of an array (from a pointer pointing to the first element array)?
(17 answers)
Closed 8 years ago.
I want to use a function that receive a pointer to an array together with its size and sets all element value to zero.
But the value of the array was checked in the function, the value is not 1234 which should be 1234 if correct, because the input value is 1234.
Just want to know where's the mistake in my following code.
#include <stdio.h>
#include <stdlib.h>
void receive(int *point, const int size);
int main(void) {
int a;
int *b;
scanf("%d", &a);
b=(int*)malloc(sizeof(int)*a);
printf("size of input: %d\n", sizeof(b));
receive(b, sizeof(b));
free(b);
return 0;
}
void receive(int *point, const int size) {
int c=sizeof(*point);
printf("filling the array to zero");
int i;
for (i=0; i<c; i++) {
printf("\n previous_value:%d\n", point[i]);
point[i]=0;
printf(", current_value %d\n", point[i]);
}
}
I changed a few incorrect statements, the resulting code is:
#include <stdio.h>
#include <stdlib.h>
void receive(int *point, const int size);
int main(void) {
int a;
int *b;
scanf("%d", &a);
b= malloc(sizeof(int)*a); //removed implicit declaration of malloc return value
printf("size of input: %d\n", a);
receive(b, a); //changed sizeof(b) to a as second argument of function
free(b);
return 0;
}
void receive(int *point, const int size) {
//removed int c = sizeof(*point); unnecessary statement
printf("filling the array to zero");
int i;
for (i=0; i<size; i++) { //substituted c with size
printf("\n previous_value:%d\n", point[i]);
point[i]=0;
printf(", current_value %d\n", point[i]);
}
}

Resources