By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,854 Members | 849 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,854 IT Pros & Developers. It's quick & easy.

type of argument in printf function

P: 5
I am trying to use swaps function and to initiate steps for a bubble sort. In the line 17 I tried to write `printf("%d", &nombre);`. and `printf("%d", nombre);` which respectively leed to the following warning, pasted after the snippet.


I cannot understand how to write the argument in the printf function. Can someone help me?






Expand|Select|Wrap|Line Numbers
  1.   #include <stdio.h>
  2.  2 void swaps(int *a, int *b);
  3.  3
  4.  4 int main()
  5.  5
  6.  6 {
  7.  7   int card, i, j;
  8.  8
  9.  9   printf("combien?\n");
  10. 10   scanf("%d", &card);
  11. 11   int nombre[card];
  12. 12   for (int i=0; i< card; i++)
  13. 13   {
  14. 14
  15. 15     printf("inscrivez un nombre.>
  16. 16     scanf("%d", &nombre[i]);} 
  17. 17     printf("%d", &nombre);
  18. 18     int fois = card/2;
  19. 19     while(fois <=0)
  20. 20    {
  21. 21      for(int i = 0; i < card-1; i++)
  22. 22      {
  23. 23       swaps(&nombre[i], &nombre[i+1]);
  24. 24      }
  25. 25    fois--;
  26. 26   }
  27. 27
  28. 28 for(int i = 0; i< sizeof(nombre)>
  29. 29   {
  30. 30      printf("%d", nombre[i]);
  31. 31   }
  32. 32 return 0;
  33. 33 }
  34. 34
  35. 35 void swaps(int *a, int *b)
  36. 36 {
  37. 37   int temp;
  38. 38   if (*a > *b)
  39. 39   {
  40. 40     temp = *a;
  41. 41     *a = *b;
  42. 42     *b = temp;
  43. 43   }
  44. 44
  45. 45 }
  46.  
  47.  
  48.  







`bu.c:17:18: warning: format
specifies type 'int' but the
argument has type 'int *'
[-Wformat]
printf("%d", nombre);
~~ ^~~~~~
1 warning generated.
$ nano bu.c
$ clang bu.c -o bu
bu.c:17:18: warning: format
specifies type 'int' but the
argument has type 'int
(*)[card]' [-Wformat]
printf("%d", &nombre);
~~ ^~~~~~~
1 warning generated.`
4 Weeks Ago #1

✓ answered by dev7060

When an array is declared, the name is a pointer to the first element of the array. So, here 'nombre' is an int type pointer (int*). %d format specifier is used to display int type values, not for printing addresses (%p is used for that). Here both nombre and &nombre denote addresses, not some int type value.

Share this Question
Share on Google+
2 Replies


100+
P: 149
When an array is declared, the name is a pointer to the first element of the array. So, here 'nombre' is an int type pointer (int*). %d format specifier is used to display int type values, not for printing addresses (%p is used for that). Here both nombre and &nombre denote addresses, not some int type value.
4 Weeks Ago #2

P: 5
I reached it!

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. //Compiler version gcc  6.3.0
  5. int swaps(int *num1, int *num2);
  6. int main()
  7.   int card;
  8.   printf("How many elements?\n");
  9.   scanf("%d", &card);
  10.   printf("The length of the array will be %d\n", card);
  11.   int numbers[card];
  12.   int *num = NULL;
  13.   num = numbers;
  14.  
  15.     for (int i = 0; i<card; i++)
  16.     {
  17.       printf ("enter the %d indexed number\n", i);
  18.       scanf ("%d", num+i);
  19.       printf("\nThe list of numbers is ");
  20.     }
  21.     for(int j=0; j< card; j++)
  22.     printf("%d, ", *(num + j);
  23.  
  24.     for(int a = 0; a < card; a++)
  25.     {
  26.       for(int b = a+1; b < card; b++)
  27.        {
  28.          if *(num +a) > *(num +b))
  29.          swaps(num+a, num+b);
  30.        }
  31.     }
  32.     printf("\nThe new list of numbers is ");
  33.     for(int j=0; j< card; j++)
  34.     printf("%d, ", *(num+j));
  35.  
  36.  
  37.   return 0;
  38. }
  39.  
  40. int swaps(int *num1, int *num2)
  41. {
  42.   int temp;
  43.   temp = *num1;
  44.   *num1 = *num2;
  45.   *num2 = temp;
  46. }
  47.  
4 Weeks Ago #3

Post your reply

Sign in to post your reply or Sign up for a free account.