a program that squares elements

Asked 1 years ago, Updated 1 years ago, 49 views

It's a question that was asked in the class assignment.
In order to create a procedure sqarray to store the square of element number i in data[i] for an integer array data, I programmed it as follows:

/*
ex1401.c
*/

# include <stdio.h>

void sqarray (int data[], int size)
{
  inti;
for(i=0;i<size;i=i+1){
printf("%d", & data[i]);
}
}

void printIntArray(inta[], int size)
{
  inti;
for(i=0;i<size;i=i+1){
printf("%d", a[i]);
}
printf("\n");
}


int main (void)
{
  int data [100], n;

  printf("n=? ");
  scanf("%d", & n);

  sqarray(data,n);
  printIntArray(data,n);


return 0;
}

However, I received an error similar to the following:

ex1401.c:Infunction'sqarray':
ex1401.c:11:10:warning:format '%d' expectations argument of type 'int', but argument 2 has type 'int*' [-Wformat=]
 printf("%d", & data[i]);
         ~^   ~~~~~~~~
         %ls

"In the above indication, I thought ""%d"" would look like ""%ls"", so I fixed it, but I got an error again."
What's wrong with my program?

c

2022-09-30 21:44

1 Answers

&data[i] should be fixed.

warning:format '%d' expectations argument of type 'int', but argument2 has type 'int*'

This warning is to note that the argument expected a value of type int but was actually passed a value of type int*.Now that you want to see the i element of the array data (probably for debugging?), &data[i] should be passed to printf instead of &data[i].


2022-09-30 21:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.