0

I want to have a constant character array of which each array element to be passed to function in runtime. I have written them in following way:

const char *IntenistyVal[] ={"1","2","3","4","5"};

and The function is

Test(const char *pText) 

(I cannot change this as this is one of standard Library function). Now when I try to call the function "Test" as

Test(IntensityVal[0])

I also Tried

const char * const IntenistyVal[] ={"1","2","3","4","5"};

In both cases I am getting error as "Expression must have a constant value". Can Any one help me where I am doing Wrong.

alk
  • 69,737
  • 10
  • 105
  • 255
  • Could it be the typo in the array declaration? `IntenistyVal` versus `IntensityVal` – MrPublic Nov 15 '16 at 18:35
  • 2
    You have an array of pointers-to-char there, not an array-of-char. – Jens Nov 15 '16 at 18:35
  • MSVC works perfectly fine after correcting the typo, assuming you wanted an array of strings and not a single string. – Weather Vane Nov 15 '16 at 18:40
  • @ Mr Public: Sorry! Yup it is Typo – Nagendra Babu Nov 15 '16 at 18:40
  • @Weather Vane: Yes exactly I Need array of strings – Nagendra Babu Nov 15 '16 at 18:42
  • 3
    The error messages dues to something else, you do not show us. `const char * strs[] = {"foo", "bar"}; void test(const char * p) {}; int main(void) { test (strs[0]); return 0; }` is perfectly valid C. – alk Nov 15 '16 at 18:48
  • That should not be a problem. See http://ideone.com/N6ZcYS. – R Sahu Nov 15 '16 at 18:49
  • @ alk: It works fine When I call Test as Test("Hello"); Does this is giving any clue? – Nagendra Babu Nov 15 '16 at 18:52
  • @ R Sahu: Thanks But This is Not working for me in my compiler. I am using CCS (code composer studio) . – Nagendra Babu Nov 15 '16 at 18:55
  • @alk: Sorry I am getting same error with your example too. Compiler is CCS for embedded platform. – Nagendra Babu Nov 15 '16 at 19:02
  • Are you really using `= {"1","2","3","4","5"};` to initialise your pointer array, or could it be you are using variables instead of the literals (as you show here) inside your "real" code? – alk Nov 15 '16 at 19:11
  • I am with @alk take a look at http://ideone.com/4Ybcxp – Tomer W Nov 15 '16 at 19:13
  • What line does the "Expression must have a constant value" refer to? Please post a small compilable complete program (with a main()) that causes the error message. – Jens Nov 15 '16 at 19:39
  • "Test(const char *pText) ... is one of standard Library function)" is unclear. `Test()` is _not_ a standard library function. Post its declaration. – chux - Reinstate Monica Nov 15 '16 at 19:48
  • Does `Test("Hello");` work? – chux - Reinstate Monica Nov 15 '16 at 19:50
  • This Is my actual Code: Canvas(NeckIntensity,GFX_Panels + Intensity1, &MidBackIntensity, 0, &g_sKentec320x240x16_SSD2119, 75, 70, 20, 50, CANVAS_STYLE_TEXT | CANVAS_STYLE_TEXT_OPAQUE | CANVAS_STYLE_TEXT_VCENTER | CANVAS_STYLE_TEXT_HCENTER, 0, 0, ClrWhite,&g_sFontCm18b,IntenistyVal[CurrIntensity.Neck], 0, 0); – Nagendra Babu Nov 15 '16 at 20:16
  • This Is my actual Code: 1). Canvas(NIntensity,GFX_Panels + Intensity1, &MidIntensity, 0, &g_sKentec320x240x16_SSD2119, 75, 70, 20, 50, CANVAS_STYLE_TEXT | CANVAS_STYLE_TEXT_OPAQUE | CANVAS_STYLE_TEXT_VCENTER | CANVAS_STYLE_TEXT_HCENTER, 0, 0, ClrWhite,&g_sFontCm18b,IntenistyVal[CurrIntensity.NInten], 0, 0); 2). const char * IntenistyVal[] ={"1","2","3","4","5"}; The Standard library I was talking about is Tiva C GrLib. – Nagendra Babu Nov 15 '16 at 20:22
  • From Library: #define Canvas(sName, psParent, psNext, psChild, psDisplay, i32X, i32Y, i32Width, i32Height, ui32Style, ui32FillColor, ui32OutlineColor, ui32TextColor, psFont, pcText, pui8Image, pfnOnPaint) tCanvasWidget sName = CanvasStruct(psParent, psNext, psChild, psDisplay, i32X, i32Y, i32Width, i32Height, ui32Style, ui32FillColor, ui32OutlineColor, ui32TextColor, psFont, pcText, pui8Image, pfnOnPaint) – Nagendra Babu Nov 15 '16 at 20:23
  • Please add the code form the comments as an update to your question. As it stand its nod readable. – alk Nov 16 '16 at 13:02

1 Answers1

0

A const array of char would be

 const char IntensityVal[] ={'1', '2', '3', '4', '5', 0};

The final 0 is important if you want to pass it as a string. But I'm not sure if this is what you really want. It would help if you didn't hide the actual function as Test, but tell us the actual Standard Library function and what you want to achieve (i.e you have an XY-Problem).

Community
  • 1
  • 1
Jens
  • 69,818
  • 15
  • 125
  • 179