We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi, I have the following chunk of code: unsigned char code A[4]={3,'L','P','L'} unsigbed char code B[3]={2,'P','P'} If a customer by means of a keyboard, writes the letter A in a variable a pointer must point for the Array A[4], if instead writes the B letter, must point for the Array B. How can I realize this function with a Pointer? Best regard Antonello
unsigned char code A[4]={3,'L','P','L'}; unsigned char code B[3]={2,'P','P'}; char customerChar; unsigned char code *p; if (customerChar == 'A') p = A; else if (customerChar == 'B') p = B;
Quibble: the above code falls through with an undefined value for p if customerChar is neither A nor B. It should have another else for error handling, or perhaps a default value:
p = A; if (customerChar == 'B') { p = B; }
switch (customerChar) { case 'A' : p = A; break; case 'B' : p = B; break; default: // unexptected value for customer char // error handling code goes here p = NULL; break; }
/* ... */