-5

What I am asking here can be very easy to solve. Code works fine but this warning is bugging me!

//initailize array elements
    char ZeroA[6]   = {0xC0,0x07,0x40,0x04,0xC0,0x07,};
    char OneA[6]    = {0x80, 0x04, 0xC0, 0x07, 0x00, 0x04,};
    char TwoA[6]    = {0x40, 0x07, 0x40, 0x05, 0xC0, 0x05,};
    char ThreeA[6]  = {0x40, 0x05, 0x40, 0x05, 0xC0, 0x07,};
    char FourA[6]   = {0x80, 0x03, 0x00, 0x02, 0x80, 0x07,};
    char FiveA[6]   = {0xC0, 0x05, 0x40, 0x05, 0x40, 0x07,};
    char SixA[6]    = {0xC0,0x05,0x40,0x05,0x40,0x07,}; 
    char SevenA[6]  = {0x40,0x04,0x40,0x03,0xC0,0x00,};
    char EightA[6]  = {0xC0,0x07,0x40,0x05,0xC0,0x07,};
    char NineA[6]   = {0xC0,0x05,0x40,0x05,0xC0,0x07,};
    char TenA[6]    = {0x00,0x01,0x80,0x03,0x00,0x01,};

int *mCount;     //address holder
char var = 4;    //Just random number for illustration

int *XYZ[11]={&ZeroA,&OneA,&TwoA,&ThreeA,&FourA,&FiveA,&SixA,&SevenA,&EightA,&NineA,&TenA};

mCount = XYZ[Var];   
nikhil patil
  • 142
  • 1
  • 12
  • 1
    As well it should. What are you trying to do, and why are you trying to do it like this? On how many platforms is `int` 6 bytes wide? – Lightness Races in Orbit May 04 '17 at 20:15
  • 1
    XYZ is an array of pointers to `int` the arrays you are taking the addresses of are `char`. Problem? – infixed May 04 '17 at 20:17
  • It doesn't compile http://ideone.com/ndU7Xz – imreal May 04 '17 at 20:17
  • I have 10 variables like XYZ. I want to access any of the arrays of XYZs – nikhil patil May 04 '17 at 20:18
  • @imreal it does compile correctly and works – nikhil patil May 04 '17 at 20:18
  • @Nikhil No it does not. Follow the link I posted. And it has so many compile errors is hard to tell which one are you asking about. – imreal May 04 '17 at 20:19
  • If it compiles for you, [this must be C](https://ideone.com/anPGQC). Why did you tag the question [tag:c++] (a language in which this code produces many, many compilation errors)? – Lightness Races in Orbit May 04 '17 at 20:23
  • my compiler output looks like this : vxs.c:67:1: warning: initialization from incompatible pointer type vxs.c:67:1: warning: initialization from incompatible pointer type vxs.c:67:1: warning: initialization from incompatible pointer type BUILD SUCCESSFUL (total time: 5s) Loading code from C:/Users/nikhil/blah/dist/default/production/blah.production.hex... Loading completed – nikhil patil May 04 '17 at 20:24
  • @BoundaryImposition I am Sorry errors like Var is not initialized are because i edited lil code while posting here – nikhil patil May 04 '17 at 20:27
  • You are getting warnings because you are storing pointers to `char `into pointers to int. While the program may do what you want, it is likely to break on some other platform. For instance, if an `int` is 8 bytes long, then your fetch of 8 bytes at a time as an `int` it will run 2 bytes past the end of any of the 6 byte arrays. If you do that to `TenA` you can't even guess what the extra two bytes will be. – infixed May 04 '17 at 20:28
  • @infixed array i m taking in is 24bit wide address – nikhil patil May 04 '17 at 20:32
  • @BoundaryImposition: Can you provide a reference where the standard disallows this? – too honest for this site May 04 '17 at 20:33
  • Simply put: An array is not a pointer! – too honest for this site May 04 '17 at 20:34
  • In C, there is no implicit conversion between `int*` and `char(*)[6]`. You cannot legally use a `char(*)[6]` value to initialize an `int*` object. It's a *constraint violation*, which is as close as C gets to calling something illegal. It happens that some compilers issue non-fatal warnings for some constraint violations, particularly for nonexistent implicit conversions like this one. That doesn't mean you shouldn't take such a warning as seriously as a fatal error message. What you're trying to do is not valid in C. If you'll describe what you're trying to accomplish, we can probably help. – Keith Thompson May 04 '17 at 20:35
  • In particular, `XYZ[0]`, for example, is a pointer object of type `int*`; it points to an `int` object. What exactly do you want it to point to? `ZeroA` is not an `int` object, so it can't point to that. – Keith Thompson May 04 '17 at 20:37
  • So if you are trying to define a 24bit address a byte at a time, then are you aware that 24 bits is 3 bytes, not 6? What with endianess and other problems, I don't even know where to begin. – infixed May 04 '17 at 20:39
  • @keithThompson Yes I am trying to create an array of arrays – nikhil patil May 04 '17 at 20:43
  • Change the two places where you declare things `int *` into `char *` and the warning will go away. Will it still 'work', who knows, because you have not defined what 'work' means – infixed May 04 '17 at 20:50
  • @Olaf: The C++ standard? I can't be arsed to look it up but the error messages speak for themselves really. There's no reason to expect a `char (*)[6]` to convert to an `int*` – Lightness Races in Orbit May 04 '17 at 21:05
  • @Nguaial When I said it works it means in simulated Microcontroller I can Assign suupose XYZ[0] to mCount and check elements at mCount, mCount+1 and so on and I can see in all the elements in the debugger just fine!!!! – nikhil patil May 04 '17 at 21:16
  • @BoundaryImposition: 1) The questions tagged C, not C++ (it was already when I wrote my comment) 2) "On how many platforms is int 6 bytes wide? – BoundaryImposition" 3) If that was meant to be rude ("to arse" - seriously?), you should be more explicit. – too honest for this site May 04 '17 at 21:24
  • @Olaf: 1) Yes, I'm the one who removed the C++ tag, _because_ it's invalid C++ and thus the OP must be asking about C. Then I don't know why you're asking me which part of the C standard prohibits the program, since I never claimed that it does. I think there's been a misunderstanding. 2) What about it? 3) No, that is not rude; it is an English idiom meaning "I don't have time / I can't be bothered right now". – Lightness Races in Orbit May 04 '17 at 21:38

2 Answers2

0

With the continuation from what amine.ahd left off, mCount needs to be a char pointer.

char *mCount;     //address holder

mCount = XYZ[var];   

int i = 0;

for( i = 0; i < 6; i ++ )
 printf( "%c ", mCount[i] );

Inserted a few lines of code to test. Although it printed white characters, it seems to be working. More importantly, there are no compilation warnings.

Nguai al
  • 958
  • 5
  • 15
-2

ZeroA and so one are pointers to an array of char elements. &ZeroA holds the address of the pointer to the array ZeroA and so to hold it you need char **

The correct way to do it in your example is like this: char *XYZ[11]={ZeroA,OneA,TwoA,ThreeA,FourA,FiveA,SixA,SevenA,EightA,NineA,TenA};

amine.ahd
  • 431
  • 3
  • 11
  • 2
    No, `&ZeroA` is not the address of a pointer. It's the address of an array. (Remember that arrays are not pointers.) – Keith Thompson May 04 '17 at 20:37
  • `ZeroA` points to the first element of the array (i.e a pointer...), `&ZeroA` is the address of that pointer. – amine.ahd May 04 '17 at 20:43
  • 1
    `ZeroA` is an array, not a pointer! `&ZeroA` is a pointer to array! An array is not a pointer. – too honest for this site May 04 '17 at 20:49
  • `ZeroA` is a pointer to the first element of the array. What is so hard to understand? – amine.ahd May 04 '17 at 20:49
  • @amine.ahd `ZeroA` is a `char[6]` which can decompose to a `char*`. However, `&ZeroA` is a `char[6]*` which cannot decompose to `char**` because *an array is not a pointer*. (not sure I got the type notation correct but I hope my point gets across) – kmdreko May 04 '17 at 21:10
  • @amine.ahd with `char ZeroA[6] = { };` declaration and initialization, ZeroA represents the address of array which is same as saying address of the first element of ZeroA which is ZeroA[0]. So `ZeroA = &ZeroA[0]`. You are right that ZeroA points to the first element but it is not a pointer. Lets say char *cp; cp = ZeroA; In this case cp is a pointer. to the array. Although ZeroA has behavior like a char pointer, there is a difference. e.g. sizeof(cp) equals to 8 but sizeof ZeroA is 4. – Nguai al May 04 '17 at 21:11
  • When I said it works it means in simulated Microcontroller I can Assign suupose XYZ[0] to mCount and check elements at mCount, mCount+1 and so on and I can see in all the elements in the debugger just fine!!!! – nikhil patil May 04 '17 at 21:20
  • Address of ZeroA exists. But what is &ZeroA? Address of address? I just ran a code. ZeroA and &ZeroA print out same addresses. `printf( "address of an array = %p address of address of array = %p\n", TenA, &TenA );`. It prints out:` address of an array = 0x7ffe76a9bce0 address of address of array = 0x7ffe76a9bce0` To me &ZeroA has no meaning in this case. What do you think? – Nguai al May 04 '17 at 21:24
  • Yes you are right! still the question remain What I am Trying to do here is i want to assign array directly to mCount I can do it may be with other ways but i dont want to assign elements one by one which takes 12 cycles compared to 2 cycles in here! – nikhil patil May 04 '17 at 21:26
  • @amine.ahd: "`ZeroA` points to the first element of the array (i.e a pointer...)" -- No, that's incorrect. `ZeroA` doesn't point to anything. It's an array object. When its name is used in an expression it is, in most but not all contexts, converted to a pointer to that array object's initial element. Defining `char ZeroA[6] = ...;` creates an array object; it does not create a pointer object. `&ZeroA` is the address of that array object. `&ZeroA[0]` is the address of the initial element of that array object (same memory location, different type). – Keith Thompson May 04 '17 at 21:45
  • 1
    @Nguaial: "`ZeroA` represents the address of array which is same as saying address of the first element of `ZeroA` which is `ZeroA[0]`." Wrong, wrong, and wrong. – Keith Thompson May 04 '17 at 21:46
  • @KeithThompson - Thanks for the link. But the addresses of Zero and &Zero[0] are the same. If I have failed to understand this, what am I missing? – Nguai al May 04 '17 at 21:57
  • 1
    _"ZeroA is a pointer to the first element of the array. What is so hard to understand?"_ It's not "so hard to understand"; it is, however, incorrect. Check your own facts before getting arsey! – Lightness Races in Orbit May 04 '17 at 21:59
  • 1
    @Nguai: _"If I have failed to understand this, what am I missing?"_ The type system. – Lightness Races in Orbit May 04 '17 at 21:59
  • 3
    @Nguaial: `ZeroA` is an array object. Its address, `&ZeroA` is of type `char(*)[6]`. `ZeroA[0]` is an object of type `char`. Its address, `&ZeroA[0]` is of type `char*`. Same memory location, different type. When the name `ZeroA` is used in an expression, it is *in most contexts* implicitly converted ("adjusted" would be more accurate) to a pointer expression, yielding the same value as `&ZeroA[0]`. You wrote "`ZeroA` represents the address of array". No, `ZeroA` *is* an array. – Keith Thompson May 04 '17 at 22:40