Thread: arrays of int, parsing, changing one element

  1. #1
    Registered User
    Join Date
    Nov 2001

    arrays of int, parsing, changing one element

    Dear Fellow coders

    I have an array of integers in main() and all elements are given a zero value
    int counts[10] = {0,0,0,0,0,0,0,0,0,0}

    I am parsing a pointer to this array to another function:

    1) void anotherfunction(int *num)
    2) {
    3) (num)++; /*Compiles without messages but does not increment value to one*/
    4) printf(“%d”, *num); /*Prints zero*/
    5) }
    from within anotherfunction() I want to increment the value in element 0, (counts[0]) by one and then print it out. So far this has not worked.

    I have tried reading my original study manual, but this does not include an example of incrementing the value inside an element using “++”. It shows how to move along the array using pointer arithmetic.

    I have also tried many variations as follows:
    These produce a message “L value required”. My compiler says “An lvalue is an object locator: an expression that designates an object. An example of an lvalue expression is *P, where P is any expression evaluating to a non-null pointer.
    A modifiable lvalue is an identifier or expression that relates to an object that can be accessed and legally changed in memory.”
    Wow. However, I am sorry to say I do not properly understand this.

    I have performed a step through/watch and seen that line 3 does not increment element zero.

    I am using Borlands DOS Turbo C++ V 3.0 complier

    Any help or guidance appreciated


  2. #2
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    You must dereference the pointer first - then you can increment the value the pointer is pointing at.
    The following code works fine with Borland Command Line Tools 5.5.1.

    #include <stdio.h>
    void func(int *num);
    int main()
        int array[10] = {0};
        return 0;
    void func(int *num)
        printf("%d", *num);

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    If you want to change other elements in the array you need to pass the number of elements in the array:

    #include <stdio.h>
    void func(int *array, int nofelements);
    void func(int *array, int nofelements)
       int i;
       for(i = 0; i < nofelements; i++)
          array[i] = i;
    int main(void)
       int i;
       int array[10] = {0,0,0,0,0,0,0,0,0,0};
       func(array, sizeof(array)/sizeof(*array));
       for(i = 0; i < sizeof(array)/sizeof(*array); i++)
          printf("array[%d] = %d\n", i, array[i]);
       return 0;

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    >>int counts[10] = {0,0,0,0,0,0,0,0,0,0};
    This is better written:
    >>int counts[10] = {0};
    All remaining elements are initialised to 0 by default.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  3. Replies: 26
    Last Post: 11-30-2007, 03:51 AM
  4. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM