Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
Hi,

Here is my code -
C++
static char BASED_CODE szFilter[] = "HTML Files (*.xls)|*.xls|All Files (*.*)|*.*||";
 
    if (AppMarketID == 12)
    { 
      szFilter = "HTML Files (*.xls)|*.xls|All Files (*.*)|*.*||";
    }
CFileDialog dia(FALSE, "", default_name, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter);

I’m getting an error as -
error C2440: '=' : cannot convert from 'const char [49]' to 'char [47]'

Can you please guide me how can I assign value to 'szFilter' dynamically as above?
Posted
Updated 18-Apr-11 1:18am
v3
Comments
[no name] 18-Apr-11 7:04am    
Use
const char local_string_1[] = "HTML Files (*.xls)|*.xls|All Files (*.*)|*.*||";
const char local_string_2[] = "HTML Files (*.xls)|*.xls|All Files (*.*)|*.*||";
char szFilter[500];
strcpy(szFilter,local_string_1);
strcpy(szFilter,local_string_2);

to get rid of warning C4996: 'strcpy' you should use
strncpy() instead of strcpy()

because if you are using strcpy and "source" string pointer is not pointing to a NULL terminated string then srtcpy may go beyond the boundaries of source or/and destination buffer, which is unsafe and that's because strcpy() is depreciated.

but in strncpy(dest , src ,n) at most n bytes of src are copied into dest.

good luck. :)
 
Share this answer
 
static char BASED_CODE szFilter[] = "HTML Files (*.xls)|*.xls|All Files (*.*)|*.*||";

is an array and cannot be changed by an assignment statement, you should code it as:
static char BASED_CODE *szFilter = "HTML Files (*.xls)|*.xls|All Files (*.*)|*.*||";

Incidentally, why are you trying to change it to the exact same string?
 
Share this answer
 
Comments
pjhelp 18-Apr-11 7:25am    
Thanks a lot for your quick response.
Here i'm not trying to change the exact string.If you see carefully you can find
i'm changing to "HTML Files (*.xlsx)|*.xlsx|All Files (*.*)|*.*||" from "HTML Files (*.xls)|*.xls|All Files (*.*)|*.*||" accoring to the requirement.

Thanks
Richard MacCutchan 18-Apr-11 7:56am    
No you are not; take another look at what you have posted.
Bharat Kumar Arya 19-Apr-11 0:48am    
What ever that was, just don't argue on the strings...

he wants to dynamically change the string so providing a solution is better...
either using strcpy_s or using static constant string as you have commentated... :)
Richard MacCutchan 19-Apr-11 5:07am    
What is the point of dynamically changing static constants? I'm trying to educate the OP in the correct use of language features in order to avoid big problems in the future.
Sergey Alexandrovich Kryukov 18-Apr-11 15:07pm    
Good, a 5.
--SA
Its true you will be getting error if you code like this:

Please change this as:
if (AppMarketID == 12)
{
  //Please check on size or provide it large when defining szFilter.
  strcpy(szFilter, "HTML Files (*.xls)|*.xls|All Files (*.*)|*.*||");
}

You are changing content of a const char pointer(szFilter) with another const char pointer ("HTML Files (*.xls)|*.xls|All Files (*.*)|*.*||") ...

Hopy this might help!!! :)
 
Share this answer
 
Comments
[no name] 18-Apr-11 7:13am    
szFilter must have enouph size to do in this way.
Guyverthree 18-Apr-11 7:21am    
indeed and seconded.

J
Bharat Kumar Arya 18-Apr-11 7:24am    
I just want to make it sure as he want to change it dynamically., he may copy another longer string :)..
Otherwise its ok...
pjhelp 18-Apr-11 7:28am    
Thanks for your guidance .Now i'm not getting any error ,but getting an Worning as -

warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

Can you please guide me how can i avoid this Worning
Thank you .
Richard MacCutchan 18-Apr-11 7:58am    
Don't use arrays or strcpy; create a pointer that can be changed with an assignment statement as mentioned in my answer above.
#define MAX_LEN  500  //or as much text as you want...
static char BASED_CODE szFilter[MAX_LEN + 1] = "HTML Files (*.xls)|*.xls|All Files (*.*)|*.*||";
if (AppMarketID == 12)
{
  strcpy_s(szFilter, MAX_LEN, "HTML Files (*.xls)|*.xls|All Files (*.*)|*.*||");
}

this is just limiting the copying upto some max len... thus protect you from ill-behavior of strcpy which stops copying only when finds a '\0', NULL character...

Hope this might help!!! :)
 
Share this answer
 
Comments
Richard MacCutchan 18-Apr-11 9:55am    
Why waste time with strcpy()? Both of these strings are constants so all he needs is a single pointer that can be set to one or other of the string constants. KISS!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900