-3

C novice here.
I am trying to write a program containing function that takes in an array pointer, and each element of the array passed in the main function is incremented by 1. This is what I have tried:

#include <stdio.h>
#include <stdint.h>

void array_incr(int8_t *, uint8_t);

int main (void){
    int8_t *arr[] = {0xAB, 0xCB, 0xC4, 0x84};
    array_incr(arr, sizeof(arr)/sizeof(arr[0]);
    int i;
    for (i = 0; i < 4; i++)
        printf("%d", arr[i]);
}

void array_incr(int8_t *arr, uint8_t len){
    int i;
    for(i=0; i<len; i++)
        arr[i]++;

}

This is compiling with a lot of warnings and errors. Could someone please tell me where I am going wrong, while maintaining the same format of implementation?

abruzzi26
  • 159
  • 1
  • 10
  • 1
    *"Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve)."* – Brynden Nov 02 '16 at 20:25
  • One pile of warnings is because `int8_t *arr[]` is an array of pointers, not an array of (small) integers. Drop the `*`. The variable `min_ptr` is unused and should be removed. You should probably not use both `sizeof(arr)/sizeof(arr[0])` and 4 for the size of the array; the expression is better than number. – Jonathan Leffler Nov 02 '16 at 20:25
  • Why do you compute the size of the array to call `array_mod`, but hard-code it to print it? – Scott Hunter Nov 02 '16 at 20:26
  • 2
    The errors and warnings are *telling you* what's going wrong. Try reading them! (Also, when developing new code, always compile with all warnings enabled — add `-Wall -pedantic` to the command line.) – r3mainer Nov 02 '16 at 20:26
  • And `0xC4` exceds the range of `int8_t` (typicaly -128 ... 127) – David Ranieri Nov 02 '16 at 20:28
  • @KeineLust: "**typicaly** 0 ... 127"?? - **exactly** -128..127 and nothing else! – too honest for this site Nov 02 '16 at 20:30
  • @KeineLust: Still incorrect after the edit! – too honest for this site Nov 02 '16 at 20:36
  • @Olaf Yes, I was thinking in `unsigned char`, `uint8_t` is _always_ in the range -128 ... 127, thank you – David Ranieri Nov 02 '16 at 20:41
  • @KeineLust: Before we get the discussion warning: You might want to read about the valid ranges for the standard integer types as well as the extended integer types. Your asumption for `uinsigned char` is wrong, too. Just in case you have "Lust". – too honest for this site Nov 02 '16 at 20:43
  • @Olaf, are you sure? if I remember well, in one's complement encoding the range of `signed char` was -127 ... 127 (there were + 0 and -0 in different values) , anyway you are right, `int8_t` must be encoded as a two's complement signed integer, the range is always -128 ... 127 – David Ranieri Nov 02 '16 at 21:10
  • @KeineLust: You wrote about **`unsigned`** `char`! And please read the standard about fixed width types. – too honest for this site Nov 02 '16 at 21:25
  • @Olaf, ooops, I am totally dyslexic :) – David Ranieri Nov 02 '16 at 21:31

2 Answers2

1

Multiple problems indeed:

  • Incorrect type in declaration int8_t *arr[] = {0xAB, 0xCB, 0xC4, 0x84};: you should remove the * as arr is an array of numbers, not an array of pointers. Furthermore, the values are larger than 127 which is the maximum value for type int8_t, you should use uint8_t or a larger type.

  • Missing ) at the end of array_incr(arr, sizeof(arr)/sizeof(arr[0]);

  • There is no \n in the printf("%d", arr[i]); output statement. The values will come out in a single sequence of digits (and negative signs, since the values are actually negative for type int8_t).

Here is a corrected version:

#include <stdio.h>
#include <stdint.h>

void array_incr(uint8_t *, uint8_t);

int main(void) {
    uint8_t arr[] = { 0xAB, 0xCB, 0xC4, 0x84 };
    array_incr(arr, sizeof(arr) / sizeof(arr[0]));
    int i;
    for (i = 0; i < 4; i++)
        printf("%d\n", arr[i]);
}

void array_incr(uint8_t *arr, uint8_t len) {
    int i;
    for (i = 0; i < len; i++)
        arr[i]++;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
-2

First of all, it would be best to include said list of warnings and errors, but with such a small program it's mostly alright.

Into the matter, you declared your array wrong ; it should be int8_t arr[] (you included an extra *). You want an array of integers, not a pointer to an array of integers nor an array of pointers to integers (you can't initialise the former like that anyway).

sizeof doesn't work like you think it does. It returns the number of bytes taken up by the variable or type in memory. For example, on most modern system sizeof(int) returns 4, as would something like int a = 1; sizeof(a), and just like sizeof(arr) in your case. arr is not an array, it's a pointer to an array, so it's technically an int (thus 4 bytes). You would have to create an additional variable and manually hold the array's size if it were to change.

EDIT : tried it, seems to work. I remember that it doesn't but apparently it does ...

Matrefeytontias
  • 592
  • 1
  • 4
  • 13
  • "on most modern system sizeof(int) returns 4" - wrong and dangerous to state to an obvious beginner. There are many modern 16 bit systems and some systems with e.g. 16 or 32 bits/byte. A pointer is **not** an `int`! It is not even an _integer_. Not sure what you mean with that extra variable. The `sizeof`? expression is the best part of that code. – too honest for this site Nov 02 '16 at 20:32
  • Right, I should change that. Also, it seems I'm double wrong since I tested and `sizeof(arr)` does yield the correct result. Don't know where I've got that from, I'm sorry ... – Matrefeytontias Nov 02 '16 at 20:33