Hi I was unable to find the cause of the compilation error while trying this code on Visual studio 2008. I was trying to see an example of char* return value by a function .
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static char* getActiveModuleType(void);
void main()
{
char getActiveModuleBuff[128];
//error C2106: '=' : left operand must be l-value
getActiveModuleBuff = getActiveModuleType();
printf("Active module in well formatted (2): %s\n",getActiveModuleBuff);
exit(0);
}
char* getActiveModuleType(void)
{
char activeModule[128];
int tempBuffLen=0;
int nbActivemodule = 0;
char moduleA = 1;
char moduleB = 1;
char moduleC = 0;
char moduleD = 1;
int i = 0;
if(moduleA==1) {activeModule[i] ='A'; activeModule[i+1]=','; i= i+2;}
if(moduleB==1) {activeModule[i] ='B'; activeModule[i+1]=','; i= i+2;}
if(moduleC==1) {activeModule[i] ='C'; activeModule[i+1]=','; i= i+2;}
if(moduleD==1) {activeModule[i] ='D'; activeModule[i+1]=','; i= i+2;}
printf(" Active module in : %s\n",activeModule);
//let get get the last ',' value trucated
tempBuffLen = strlen(activeModule);
nbActivemodule = tempBuffLen/2;
if((tempBuffLen == 0) && (nbActivemodule ==0)){
memcpy(activeModule,"NoActiveModule",14);
return activeModule;
}
if(activeModule[tempBuffLen-1]==',')
activeModule[tempBuffLen-1] = '\0';
printf(" Active module in well formatted : %s\n",activeModule);
return activeModule;
}
i am unable to find why this Error C2106 occurs in this code .
Help required.
thanks
You cannot assign a value to an array in C.
Use
memcpyto copy an array orstrcpy/strncpyto copy a string.Also in:
the
activeModulearray object is destroyed at the end of the function. Accessing it after the function returns is undefined behavior.