Thread: Need help converting to pointer notation

  1. #1
    Registered User
    Join Date
    Jan 2018
    Posts
    4

    Need help converting to pointer notation

    How do I change this to pointer notation?

    Code:
    #include <stdio.h>
    
    
    int addarray(int arr[],int size);
    
    
    void main() {
      int array1[10] = {1,2,3,4,5,6,7,8,9,10};
      int array2[4] = {0,0,0,0};
    
    
      printf("The sum of array1 = %d\n", addarray(array1,10));
      printf("The sum of array2 = %d\n", addarray(array2,4));
    }
    
    
    int addarray(int arr[],int size)
    {
        int sum = 0 , n ;
        for( n = 0 ; n < size ; n++ )
        {
            sum += arr[n];
        }
        return sum;
    }

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    33
    int addarray(int arr[],int size) changed to int addarray(int *arr, int size)

    sum += arr[n]; changed to sum+=*(arr + n)

  3. #3
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    277
    Quote Originally Posted by Dominiko View Post

    How do I change this to pointer notation?
    I would say change the prototype as suggested but leave the array indexing inside of the function. I mean if you're just wondering how its done, there you have it, but otherwise there's really no sense in writing code like that.
    Last edited by Sir Galahad; 01-26-2018 at 07:18 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Main returns int, not void.

    And you could try
    sum += *arr++;
    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.

  5. #5
    Registered User
    Join Date
    Jan 2018
    Posts
    4
    Thank you everybody for the help

  6. #6
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    static int addarray(int *, int);
    int main(void) {
      { int * array1;
        if ((array1 = (int *)malloc(10 * sizeof(int))) == NULL)
          exit(EXIT_FAILURE);
        for (int i = 0; i < 10; ++i)
           *(array1 + i) = i + 1;
        printf("The sum of array1 = %d\n", addarray(array1, 10));
        free(array1);
      }
    
      { int * array2;
        if ((array2 = (int *)calloc(4, sizeof(int))) == NULL)
          exit(EXIT_FAILURE);
        printf("The sum of array2 = %d\n", addarray(array2, 4));
        free(array2);
      }
    }
    
    int addarray(int * arr, int size) {
      int sum = 0;
      for (int n = 0; n < size; ++n, ++arr)
        sum += *arr;
      return sum;
    }
    Last edited by christophergray; 01-28-2018 at 03:37 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer notation
    By alien1 in forum C Programming
    Replies: 4
    Last Post: 12-02-2014, 06:19 PM
  2. Pointer notation help!
    By colmulhall in forum C Programming
    Replies: 4
    Last Post: 03-23-2011, 09:15 AM
  3. Pls Help: Insertion sort using pointer notation
    By mack_ol in forum C Programming
    Replies: 1
    Last Post: 02-17-2002, 01:03 PM
  4. manipulation of strings using pointer notation
    By mlupo in forum C Programming
    Replies: 7
    Last Post: 10-08-2001, 12:23 PM
  5. problem with pointer notation in for-loop
    By sballew in forum C Programming
    Replies: 5
    Last Post: 09-30-2001, 06:28 PM

Tags for this Thread