LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

A simple fundamental C pointer problem ?

 
 
Dear all ,
 
 I heve been facing a simple problem in my project based upon LabWindows/CVI . In the code i have declared an array of pointers and a
string pointer .
 
#include<string.h>
char *Text_Strings[]={" fg","ghhj","ws","hjk"};                       // array of pointers
char *str="subrata";  
                                                          
Now later in my code I have written 
 
strcpy(buffer,strcat(Text_Strings[2],str));
 
During compilation it's showing no error ,but at run time it is telling "array argument is small" . On the other hand in Turbo C++ it is running with out any error . could you plz tell me what's wrong in it in LW/CVI .
 
With Regards
subrata
0 Kudos
Message 1 of 2
(3,091 Views)

When you declared your string constant array Text_Strings, CVI statically allocated as much space as needed for the given strings and no more than this. For this reason you are getting the "Array too small" error while trying to strcat someting to an element of the array, since these elements cannot be extended (neither a realloc could be attempted on them!).

Supposing your buffer variable is large enough, you can substitute your compunded statement with these two:

strcpy (buffer, Text_Strings[2]);
strcat (buffer, str);

If you actually need to strcat some data to your Text_Strings array you will need to allocate enough space at the declaration level, but be aware that ANSI C allows implementations to place string constants in a memoriy region that is protected against modification, so it cannot be guaranteed that you can always modify them.

When a pointer to a writable string must be used, it is better style and more portable to inizialize an array of characters than to establish a pointer to a string constant (Harbison & Steele, "C, a reference manual". The same can be found in other C manuals).



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 2 of 2
(3,087 Views)