Warning parameter names [When I try to load my array - c

So I'm getting this message when I try to load my array using pointers.
I don't know why this keep appearing since the last program had no problem
#include<stdio.h>
#define T 10
void FLoad(int *);
void main () {
int a[T];
void FLoad(a);
}
void FLoad(int *a) {
int x;
for (x = 0; x < T; x++)
scanf("%d", a+x);
}
And here is a little program that works perfectly
#include <stdio.h>
void FImp(int *, int );
main () {
int a[] = {-10,-5,3,4}, tam;
tam = sizeof(a) / sizeof(int);
FImp(a, tam);
}
void FImp(int *a, int t) {
int x;
for (x = 0; x < t; x++)
printf("%d ",*(a + x));
putchar('\n');
}

You are using incorrect syntax when calling your function
void main()
{
int a[T];
void FLoad(a);
}
should be
void main()
{
int a[T];
FLoad(a);
}
or even better
int main(void)
{
int a[T];
FLoad(a);
}
You don't specify the function return value when you call it.

void FLoad(a);
This won't call the function. The compiler will consider this as a function declaration. So call the function without void it will work fine.

Related

f(&a) is possible to run in C?

The explanation below confused me:
When an argument is pointer to a variable x, we normally assume that x will be modified :
f(&x);
It is possible, though, that f merely needs to examine the value of x, not change it.
I tired to understand and the code below can't work.
#include <stdio.h>
void function(int& a)
{
a = 5;
}
void func(int b)
{
b = 5;
}
int main(void)
{
int x = 0;
function(x);
printf("%d", function(x));
func(x);
printf("%d", func(x));
return 0;
}
Code refer from the second answer:
int f(int &a){
a = 5;
}
int x = 0;
f(x);
//now x equals 5
int f2(int b){
b = 5;
}
int y = 0;
f2(y);
//y still equals 0
An example actually using f(&x):
#include <stdio.h>
void f(int *p) {
*p = 4;
}
int main(void) {
int x;
f(&x); // Provide a pointer to `x`.
printf("%d\n", x); // 4
return 0;
}
Both of your program use int &a, which isn't a valid C declaration. That is why they don't even compile.

Printing the elements of an array using call by reference

The purpose of show() is just to display the elements of the array. What's wrong with my code? My understanding is that we pass the address of individual elements as parameters to display() which then passes the address of "x"(which stores the address of the elements) to show(). Please help!
#include <stdio.h>
void disp(int *);
void show(int *);
int main()
{
int i;
int marks[] = {55,65,89,78,74,77,45};
for(i=0;i<=6;i++)
{
disp(&marks[i]);
printf("%d \n", (&marks[i]));
}
return 0;
}
void disp(int *x)
{
show(&x);
}
void show(int *n)
{ printf("%d\n", *(*n));
}
When you call show(&x) in disp(), you're passing in a pointer to a pointer to an int (since x is a pointer to an int), even though show() only accepts a pointer to an int.
You should change the argument in show() from int *n to int **n to fix this issue.
Well you almost got it right. Your disp function expects an address, so when you call show inside of it you shouldn't use the ampersand, because it's already an address. Then on the function show, you should use just *n instead of *(*n)
Probably this is easier like what are you trying there:
#include <stdio.h>
#define ArrSIZE 7
void disp(int *);
void show(int *);
int main(void)
{
int i;
int marks[] = {55,65,89,78,74,77,45};
for( i=0 ; i < ArrSIZE ; i++ )
{
disp(&marks[i]);
printf("%p\n", (void*)&marks[i]);
}
return 0;
}
void disp(int *x)
{
show(x);
}
void show(int *n)
{
printf("%d\n", *n);
}
But, if you insist to do it in that way, then this is what you should do:
#include <stdio.h>
#define ArrSIZE 7
void disp(int *);
void show(int **);
int main(void)
{
int i;
int marks[] = {55,65,89,78,74,77,45};
for( i=0 ; i < ArrSIZE ; i++ )
{
disp(&marks[i]);
printf("%p\n", (void*)&marks[i]);
}
return 0;
}
void disp(int *x)
{
show(&x);
}
void show(int **n)
{
printf("%d\n", **n);
}
Inside of void show(int **n) you need a pointer-to-pointer to work and remove those parentheses around n.
Like I said in my comment, if you need to print the address also you need the right specifier for that, which is %p and cast it to (void*).
Anyway both will Output this:
55
0xffefffec0
65
0xffefffec4
89
0xffefffec8
78
0xffefffecc
74
0xffefffed0
77
0xffefffed4
45
0xffefffed8
For starters according to the C Standard the function main without parameters shall be declared like
int main( void )
Secondly do not use magic numbers. They are usually a source of numerous bugs. It is better to write
size_t i;
int marks[] = { 55, 65, 89, 78, 74, 77, 45 };
const size_t N = sizeof( marks ) / sizeof( *marks );
for ( i = 0; i < N; i++ )
{
//...
In this statement
printf("%d \n", (&marks[i]));
you are trying to output a pointer (if you indeed want to output the pointer) as an object of the type int that results in undefined behavior.
There must be
printf( "%p\n", ( void * )&marks[i] );
My understanding is that we pass the address of individual elements as
parameters to display() which then passes the address of "x"(which
stores the address of the elements) to show().
In this case the function show should be declared like
void show(int **);
^^^
and defined like
void show( int **n )
{
printf( "%d\n", **n );
}

Issues with passing pointer arrays and printing them

I'm having some issues with very simple situations of passing arrays as pointers into functions and returning them. I thought I had pointers figured but I just can't get my head around it.
Here's the code:
int* getLottoDraw();
void printArray(int * array);
int find_matches(int * array1, int * array2);
int main(int argc, char *argv[])
{
int * lotteryDraw = getLottoDraw();
printArray(lotteryDraw);
system("PAUSE");
return 0;
}
int* getLottoDraw(){
int draw[6];
int i;
srand(time(NULL));
for (i = 0; i < 6; i++) {
int r = rand() % 49;
draw[i] = r;
}
return draw;
}
void printArray(int *array){
int i;
for (i = 0; i < 6; i++){
printf("%i ", array[i]);
}
}
One example output is "3 2047 4614546 0 25 45". Not what was hoping for.
You are returning a stack address, which end up being destroyed when the function ends.
Stack variables are local variables, their scope is limited to the function they're created.
They're created on the function, and destroyed when the function ends, so if you've try to access this address later you'll get undefined behavior.
You should have a dynamic allocated pointer to be able to access it outside the function, or return by value, copying the content (which can be costly in an array case).
You could do something like:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int* getLottoDraw();
void printArray(int * array);
int find_matches(int * array1, int * array2);
int main(int argc, char *argv[])
{
int * lotteryDraw = getLottoDraw();
printArray(lotteryDraw);
free(lotteryDraw);
return 0;
}
int* getLottoDraw(){
int* draw = malloc(sizeof(int)*6);
int i;
srand(time(NULL));
for (i = 0; i < 6; i++) {
int r = rand() % 49;
draw[i] = r;
}
return draw;
}
void printArray(int *array){
int i;
for (i = 0; i < 6; i++){
printf("%i ", array[i]);
}
}

Pass by reference in c array

Hi i want to ask about why i need to use printf("\n%d",x); instead of printf("\n%d",*x);?
Thank you very much
#include<stdio.h>
#include<stdlib.h>
#define totalnum 8
void display(int **);
int main()
{
int marks[totalnum]={55,65,75,85,90,78,95,60};
printf("The marks of student A are:");
display(marks);
return 0;
}
void display(int *x)
{
int i;
for(i=0;i<totalnum;i++)
printf("\n%d",x);
}
There is no pass by reference in C. The array decays to a pointer in the display function which you declared wrongly as int ** instead of int * - Compiler should have given you a warning at least about this:
http://ideone.com/R3skNj
This is how your display function should be like:
void display(int *x)
{
int i;
for(i = 0; i < totalnum; i++) {
printf("\n%d",*(x+i)); // or printf("\n%d",x[i]);
}
}
I think your looking for something like this:
#include <stdio.h>
#include <stdlib.h>
#define totalnum 8
void display(int *); //Typ is 'int *' NOT 'int **'
int main() {
int marks[totalnum] = {55,65,75,85,90,78,95,60};
printf("The marks of student A are:");
display(marks);
return 0;
}
void display(int *x) {
int i;
for(i = 0; i < totalnum; i++) {
printf("\n%d",*x); //Prints the value
x++; //increments the pointer
}
}

Passing array to function using pointer

I'm trying to print array of pointer using pointer instead of array but I got this error Segmentation fault at runtime:
enter number of element:5
array[0]=1
array[1]=2
array[2]=3
array[3]=4
array[4]=5
Segmentation fault
This is the code:
#include <stdio.h>
#include <stdlib.h>
int *array;
int n;
void input(int *array,int n);
void display(int *array,int n);
int sum(int *array,int n);
int main (void) {
int result;
printf("enter number of element:");scanf("%d",&n);
input(array,n);
display(array,n);
result=sum(array,n);
printf("sum of array=%d",result);
return 0;
}
void input(int *array,int n){
int j;
array=(int *)malloc(n*sizeof(int));
for(j=0;j<n;j++){
printf("array[%d]=",j);scanf("%d",array+j);
}
}
void display(int *array,int n){
int j;
for(j=0;j<n;j++)
printf("%d\t",*(array+j));
printf("\n");
}
int sum(int *array,int n){
int sum=0,j;
for(j=0;j<n;j++)
sum+=*array+j;
return sum;
}
How can I fixed this code? please somebody explain me what's wrong with that code.
Variable array is a local variable in function input.
As such, it is pointless to set it with array = ..., because this assignment takes effect only inside the function. You should typically pass its address (&array) to any function that needs to change it.
In your specific example, you also have a global variable array, so a quick solution to your problem would be to simply call function input without passing variable array as an argument:
void input(int n)
{
...
array = (int*)malloc(n*sizeof(int));
...
}
int main()
{
...
input(n);
...
}
Note that this is a "dirty" workaround, and you should typically strive to avoid the use of global variables.
To add the clean version to barak's answer:
int input(int ** array, const size_t n)
{
int result = 0;
assert(NULL != array);
(*array) = malloc(n * sizeof(**array));
if (NULL == (*array))
{
result = -1;
}
else
{
size_t j;
for(j = 0; j < n; ++j)
{
printf("array[%zu]=", j);
scanf("%d", (*array) + j); /* still missing error checking here . */
}
}
return result;
}
And call it like this:
if (-1 == input(&array, n))
{
perror("input() failed");
exit(EXIT_FAILURE);
}
Try this input():
void input(int **array,int n){
int j;
*array=(int *)malloc(n*sizeof(int));
for(j=0;j<n;j++){
printf("array[%d]=",j);scanf("%d",*array+j);
}
}
Because C use pass-by-value, if you want to change the value of a variable in a function, you need to pass the address of that variable as the argument to that function.
In this case, you want to change the value of array in input() and the type of array is int *, therefore the prototype of input() should be something like void input (int **array, ...).
this should do..make sure you understand what the others have said..
#include <stdio.h>
#include <stdlib.h>
int *array;
int n;
void input(int **array,int n);
void display(int **array,int n);
int sum(int **array,int n);
int main (void) {
int result;
printf("enter number of element:");scanf("%d",&n);
input(&array,n);
display(&array,n);
result = sum(&array,n);
printf("sum of array= %d",result);
return 0;
}
void input(int **array,int n){
int j;
*array= malloc(n*sizeof(int));
for(j=0;j<n;j++){
printf("array[%d]=",j);
scanf("%d",(*array)+j);
}
}
void display(int **array,int n){
int j;
for(j=0;j<n;j++){
printf("%d\t",*((*array)+j)); // you can use array notation aswell
//array[0][j] will work
}
printf("\n");
}
int sum(int **array,int n){
int sum=0,j;
for(j=0;j<n;j++){
sum += *((*array)+j);
}
return sum;
}
What does *array + j do? Does it evaluate *array and add j to it? Or does it add j to array and then dereference it? Would you be willing to bet $100 on it if I told you you are wrong?
Make your life and the life of anybody reading your code easier by using parentheses, or even better, write array [j].

Resources