type of argument in printf function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kouty
    New Member
    • Nov 2019
    • 5

    #1

    type of argument in printf function

    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?






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







    `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.`
  • dev7060
    Recognized Expert Contributor
    • Mar 2017
    • 656

    #2
    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.

    Comment

    • kouty
      New Member
      • Nov 2019
      • 5

      #3
      I reached it!

      Code:
      #include <stdio.h>
      #include <stdlib.h>
      
      //Compiler version gcc  6.3.0
      int swaps(int *num1, int *num2);
      int main()
      { 
        int card;
        printf("How many elements?\n");
        scanf("%d", &card);
        printf("The length of the array will be %d\n", card);
        int numbers[card];
        int *num = NULL;
        num = numbers;
        
          for (int i = 0; i<card; i++)
          {
            printf ("enter the %d indexed number\n", i);
            scanf ("%d", num+i);
            printf("\nThe list of numbers is ");
          }
          for(int j=0; j< card; j++)
          printf("%d, ", *(num + j);
          
          for(int a = 0; a < card; a++)
          {
            for(int b = a+1; b < card; b++)
             {
               if *(num +a) > *(num +b))
               swaps(num+a, num+b);
             }
          }
          printf("\nThe new list of numbers is ");
          for(int j=0; j< card; j++)
          printf("%d, ", *(num+j));
       
         
        return 0;
      }
      
      int swaps(int *num1, int *num2)
      {
        int temp;
        temp = *num1;
        *num1 = *num2;
        *num2 = temp;
      }
      Last edited by kouty; Nov 17 '19, 07:08 AM. Reason: using pointer

      Comment

      Working...