0

I have defined an array like this:

uint8_t brightnessvariants[4][101]={{1,2,...},{3,4,...},{5,6,...},{7,8,...}};

Now for my application i have to fill another array with one of the 4 arrays:

uint8_t brightnesstable[] = brightnessvariants[index][];

But that doesn't work. Index is counted from 0 to 3.

brightnesstable and brightnessvariants are defined in a header file as extern

How can I do it right?

7
  • Why would you want to do this? Commented Aug 23, 2016 at 21:24
  • just use a loop to access the contents of the subarray and copy each value to the new array. Commented Aug 23, 2016 at 21:26
  • 2
    @johnelemans: What's wrong with memcpy? Commented Aug 23, 2016 at 21:44
  • nothing. I like using up cycles! Commented Aug 23, 2016 at 21:47
  • @EOF I had to modify my code and "brightnesstable" is integrated and it's working fine. I just don't want to change that running system :) I just need the modification now and will optimize my code later anyway. (It's a microcontroller application) I'll check if memcpy works. Commented Aug 23, 2016 at 21:49

2 Answers 2

1

Simply do

 uint8_t brightnesstable[101]; 
 memcpy(brightnessstable, brightnessvariants[index], 101*sizeof(uint8_t));

brightnessvariants[index] is the address of the first item in the (index+1)nth row and the number of bytes you want to copy is ROWSIZE*sizeof(ITEM_SIZE).

Sign up to request clarification or add additional context in comments.

1 Comment

Suggest memcpy(brightnessstable, brightnessvariants[index], sizeof brightnesstable);
1

It really depends on what you need to do. If you need to create separate storage for a duplication of one of the rows of brightnessvariants, then you can simply declare a separate array and copy the values as discussed in the comments and the other answer.

If, however, you simply need to access one of the rows of brightnessvariants in the current scope, then there is no need for separate storage and copying. All that is required is to declare a pointer and assign the starting address of the desired row. Then you can access the desired row of brightnessvariants as if it were a separate array. e.g.

uint8_t brightnessvariants[4][101] = {{0}}, *btable = NULL;

Above, btable (short for your brightnesstable) is simply a uint8_t pointer. It can be assigned the the address of any of the rows in brightnessvariants. e.g. for the second row

btable = brightnessvariants[1];

btable can then be used to access any value within the second row, just as if it were a separate array. Here is a short example that may help:

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

int main (void) {

    uint8_t brightnessvariants[4][101] = {{0}}, *btable = NULL;
    int nrows = sizeof brightnessvariants / sizeof *brightnessvariants;

    for (int i = 0; i < 101; i++) {
        brightnessvariants[0][i] = i + 1;
        brightnessvariants[1][i] = i + 3;
        brightnessvariants[2][i] = i + 5;
        brightnessvariants[3][i] = i + 7;
    }

    for (int i = 0; i < nrows; i++) {
        printf ("\nbrightnesstable[%d] :\n\n", i);
        btable = brightnessvariants[i];
        for (int j = 0; j < 101; j++) {
            if (j && j % 10 == 0) putchar ('\n');
            printf (" %3hhu", btable[j]);
        }
        putchar ('\n');
    }

    return 0;
}

Example Use/Output

$ ./bin/ptrtobtable

brightnesstable[0] :

   1   2   3   4   5   6   7   8   9  10
  11  12  13  14  15  16  17  18  19  20
  21  22  23  24  25  26  27  28  29  30
  31  32  33  34  35  36  37  38  39  40
  41  42  43  44  45  46  47  48  49  50
  51  52  53  54  55  56  57  58  59  60
  61  62  63  64  65  66  67  68  69  70
  71  72  73  74  75  76  77  78  79  80
  81  82  83  84  85  86  87  88  89  90
  91  92  93  94  95  96  97  98  99 100
 101

brightnesstable[1] :

   3   4   5   6   7   8   9  10  11  12
  13  14  15  16  17  18  19  20  21  22
  23  24  25  26  27  28  29  30  31  32
  33  34  35  36  37  38  39  40  41  42
  43  44  45  46  47  48  49  50  51  52
  53  54  55  56  57  58  59  60  61  62
  63  64  65  66  67  68  69  70  71  72
  73  74  75  76  77  78  79  80  81  82
  83  84  85  86  87  88  89  90  91  92
  93  94  95  96  97  98  99 100 101 102
 103

brightnesstable[2] :

   5   6   7   8   9  10  11  12  13  14
  15  16  17  18  19  20  21  22  23  24
  25  26  27  28  29  30  31  32  33  34
  35  36  37  38  39  40  41  42  43  44
  45  46  47  48  49  50  51  52  53  54
  55  56  57  58  59  60  61  62  63  64
  65  66  67  68  69  70  71  72  73  74
  75  76  77  78  79  80  81  82  83  84
  85  86  87  88  89  90  91  92  93  94
  95  96  97  98  99 100 101 102 103 104
 105

brightnesstable[3] :

   7   8   9  10  11  12  13  14  15  16
  17  18  19  20  21  22  23  24  25  26
  27  28  29  30  31  32  33  34  35  36
  37  38  39  40  41  42  43  44  45  46
  47  48  49  50  51  52  53  54  55  56
  57  58  59  60  61  62  63  64  65  66
  67  68  69  70  71  72  73  74  75  76
  77  78  79  80  81  82  83  84  85  86
  87  88  89  90  91  92  93  94  95  96
  97  98  99 100 101 102 103 104 105 106
 107

Look things over and let me know if you have any questions. If you need an example of copying instead of the use of a pointer, let me know and I'm happy to help.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.