I am stuck at the last line

This is a discussion on I am stuck at the last line within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #define size 3 void show_array(int ar[],int n); void mult_array(int mult,int ar[],int n); int main(void) { static int ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    21

    I am stuck at the last line

    Code:
    #include <stdio.h>
    #define size 3
    void show_array(int ar[],int n);
    void mult_array(int mult,int ar[],int n);
    
    int main(void)
    {
      static int dip[size]={1, 2, 3};
      printf("the original dip:\n");
      show_array(dip,size);
      mult_array(2,dip,size);
      printf("the array after:\n");
      show_array(dip,size);
      system("pause");
      return 0;
    }
    void show_array(int ar[],int n)
    {
         int i;
         for(i=0;i<n;i++)
          printf("%8d",ar[i]);
         putchar('\n');
    }
    void mult_array(int mult,int ar[],int n)
    {
         int i;
         for (i=0;i<n;i++)
         *(ar++)*=mult;
     }
    the last statement is to multiply every element in dip[] by 2 and put it back, I tried to change it to (*ar)++*=mult, I expected that it will only increase the value which the pointer "ar" is pointing to. but it does not work and I also get an error from the compiler, can somebody help me? thanks

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    So what's wrong with doing

    arr[ i ] *= mult;

    ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    21
    I can get the result if I use "ar[i]*=mult;" can you tell me why? the result will be assigned to ar[i], but this is a value inside the array, can it be assigned to that?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    Inside the function, arr[ i ] is just *(arr + i )

    Arrays are always passed as pointers to the first element of the array, so whether you use array notation or pointer notation makes no difference to the compiler.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to do encryption in C
    By sankarv in forum C Programming
    Replies: 33
    Last Post: 12-28-2010, 10:01 AM
  2. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 03:13 PM
  3. adding line numbers and concatenating a filename
    By durrty in forum C Programming
    Replies: 25
    Last Post: 06-28-2008, 03:36 AM
  4. Reading a buffer line by line, while using system read
    By Hammad Saleem in forum C Programming
    Replies: 9
    Last Post: 05-27-2008, 05:41 AM
  5. Finding carriage returns (\c) in a line
    By JizJizJiz in forum C++ Programming
    Replies: 37
    Last Post: 07-19-2006, 05:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21