I want to append the length (integer) of a string message to another string using sprintf. But every time i get the above error.
void ClientToServer(const char *client_msg)
{
char str_len[20];
int len = strlen(client_msg);
char *ptr;
sprintf(ptr,"AT+CIPSEND=0,%d",len);
}
Full error:
CLEAN SUCCESSFUL (total time: 77ms)
make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf
make[1]: Entering directory 'C:/Users/acer kuttan/Documents/pk/pk/TokenOverWiFi'
make -f nbproject/Makefile-default.mk dist/default/production/TokenOverWiFi.production.hex
make[2]: Entering directory 'C:/Users/acer kuttan/Documents/pk/pk/TokenOverWiFi'
"C:\Program Files (x86)\Microchip\xc8\v2.10\bin\xc8-cc.exe" -mcpu=18F25K80 -c -fshort-double -fshort-float -memi=wordwrite -O1 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -DXPRJ_default=default -msummary=-psect,-class,+mem,-hex,-file -ginhx032 -Wl,--data-init -mno-keep-startup -mno-download -mdefault-config-bits -std=c90 -gdwarf-3 -mstack=compiled:auto:auto:auto -o build/default/production/TokMain.p1 TokMain.c
"C:\Program Files (x86)\Microchip\xc8\v2.10\bin\xc8-cc.exe" -mcpu=18F25K80 -c -fshort-double -fshort-float -memi=wordwrite -O1 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -DXPRJ_default=default -msummary=-psect,-class,+mem,-hex,-file -ginhx032 -Wl,--data-init -mno-keep-startup -mno-download -mdefault-config-bits -std=c90 -gdwarf-3 -mstack=compiled:auto:auto:auto -o build/default/production/TokenTimer.p1 TokenTimer.c
"C:\Program Files (x86)\Microchip\xc8\v2.10\bin\xc8-cc.exe" -mcpu=18F25K80 -c -fshort-double -fshort-float -memi=wordwrite -O1 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -DXPRJ_default=default -msummary=-psect,-class,+mem,-hex,-file -ginhx032 -Wl,--data-init -mno-keep-startup -mno-download -mdefault-config-bits -std=c90 -gdwarf-3 -mstack=compiled:auto:auto:auto -o build/default/production/TokenWifi.p1 TokenWifi.c
"C:\Program Files (x86)\Microchip\xc8\v2.10\bin\xc8-cc.exe" -mcpu=18F25K80 -c -fshort-double -fshort-float -memi=wordwrite -O1 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -DXPRJ_default=default -msummary=-psect,-class,+mem,-hex,-file -ginhx032 -Wl,--data-init -mno-keep-startup -mno-download -mdefault-config-bits -std=c90 -gdwarf-3 -mstack=compiled:auto:auto:auto -o build/default/production/WifiCom.p1 WifiCom.c
WifiCom.c:24:1: warning: (361) function declared implicit int
"C:\Program Files (x86)\Microchip\xc8\v2.10\bin\xc8-cc.exe" -mcpu=18F25K80 -Wl,-Map=dist/default/production/TokenOverWiFi.production.map -DXPRJ_default=default -Wl,--defsym=__MPLAB_BUILD=1 -fshort-double -fshort-float -memi=wordwrite -O1 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -msummary=-psect,-class,+mem,-hex,-file -ginhx032 -Wl,--data-init -mno-keep-startup -mno-download -mdefault-config-bits -std=c90 -gdwarf-3 -mstack=compiled:auto:auto:auto -Wl,--memorysummary,dist/default/production/memoryfile.xml -o dist/default/production/TokenOverWiFi.production.elf build/default/production/TokMain.p1 build/default/production/TokenTimer.p1 build/default/production/TokenWifi.p1 build/default/production/WifiCom.p1
C:\Program Files (x86)\Microchip\xc8\v2.10\pic\sources\c90\common\sprintf.c:13:: error: (1098) conflicting declarations for variable "_sprintf" (WifiCom.c:24)
(908) exit status = 1
make[2]: *** [dist/default/production/TokenOverWiFi.production.hex] Error 1
nbproject/Makefile-default.mk:186: recipe for target 'dist/default/production/TokenOverWiFi.production.hex' failed
make[2]: Leaving directory 'C:/Users/acer kuttan/Documents/pk/pk/TokenOverWiFi'
nbproject/Makefile-default.mk:91: recipe for target '.build-conf' failed
make[1]: Leaving directory 'C:/Users/acer kuttan/Documents/pk/pk/TokenOverWiFi'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 7s)
Edit: Warnings
The error vanished with couple of warnings when added stdio.h to the top of all header file.
CLEAN SUCCESSFUL (total time: 49ms) make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf make[1]: Entering directory 'C:/Users/acer kuttan/Documents/pk/pk/TokenOverWiFi' make -f nbproject/Makefile-default.mk dist/default/production/TokenOverWiFi.production.hex make[2]: Entering directory 'C:/Users/acer kuttan/Documents/pk/pk/TokenOverWiFi' "C:\Program Files (x86)\Microchip\xc8\v2.10\bin\xc8-cc.exe"
-mcpu=18F25K80 -c -fshort-double -fshort-float -memi=wordwrite -O1 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -DXPRJ_default=default -msummary=-psect,-class,+mem,-hex,-file -ginhx032 -Wl,--data-init -mno-keep-startup -mno-download -mdefault-config-bits -std=c90 -gdwarf-3 -mstack=compiled:auto:auto:auto -o build/default/production/TokMain.p1 TokMain.c "C:\Program Files (x86)\Microchip\xc8\v2.10\bin\xc8-cc.exe" -mcpu=18F25K80 -c
-fshort-double -fshort-float -memi=wordwrite -O1 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -DXPRJ_default=default -msummary=-psect,-class,+mem,-hex,-file -ginhx032 -Wl,--data-init -mno-keep-startup -mno-download -mdefault-config-bits -std=c90 -gdwarf-3 -mstack=compiled:auto:auto:auto -o build/default/production/TokenTimer.p1 TokenTimer.c "C:\Program Files (x86)\Microchip\xc8\v2.10\bin\xc8-cc.exe" -mcpu=18F25K80 -c
-fshort-double -fshort-float -memi=wordwrite -O1 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -DXPRJ_default=default -msummary=-psect,-class,+mem,-hex,-file -ginhx032 -Wl,--data-init -mno-keep-startup -mno-download -mdefault-config-bits -std=c90 -gdwarf-3 -mstack=compiled:auto:auto:auto -o build/default/production/TokenWifi.p1 TokenWifi.c "C:\Program Files (x86)\Microchip\xc8\v2.10\bin\xc8-cc.exe" -mcpu=18F25K80 -c
-fshort-double -fshort-float -memi=wordwrite -O1 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -DXPRJ_default=default -msummary=-psect,-class,+mem,-hex,-file -ginhx032 -Wl,--data-init -mno-keep-startup -mno-download -mdefault-config-bits -std=c90 -gdwarf-3 -mstack=compiled:auto:auto:auto -o build/default/production/WifiCom.p1 WifiCom.c "C:\Program Files (x86)\Microchip\xc8\v2.10\bin\xc8-cc.exe" -mcpu=18F25K80
-Wl,-Map=dist/default/production/TokenOverWiFi.production.map -DXPRJ_default=default -Wl,--defsym=__MPLAB_BUILD=1 -fshort-double -fshort-float -memi=wordwrite -O1 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -msummary=-psect,-class,+mem,-hex,-file -ginhx032 -Wl,--data-init -mno-keep-startup -mno-download -mdefault-config-bits -std=c90 -gdwarf-3 -mstack=compiled:auto:auto:auto -Wl,--memorysummary,dist/default/production/memoryfile.xml -o dist/default/production/TokenOverWiFi.production.elf build/default/production/TokMain.p1 build/default/production/TokenTimer.p1 build/default/production/TokenWifi.p1 build/default/production/WifiCom.p1 WifiCom.c:25:: warning: (1257) local variable "_ptr" is used but never given a value C:\Program Files (x86)\Microchip\xc8\v2.10\pic\sources\c90\common\doprnt.c:558:: warning: (1498) pointer (sprintf@sp) in expression may have no targets C:\Program Files (x86)\Microchip\xc8\v2.10\pic\sources\c90\common\doprnt.c:1465:: warning: (1498) pointer (sprintf@sp) in expression may have no targets C:\Program Files (x86)\Microchip\xc8\v2.10\pic\sources\c90\common\doprnt.c:1550:: warning: (1498) pointer (sprintf@sp) in expression may have no targets C:\Program Files (x86)\Microchip\xc8\v2.10\pic\sources\c90\common\doprnt.c:1564:: warning: (1498) pointer (sprintf@sp) in expression may have no targets ::: warning: (1311) missing configuration setting for config word 0x300003; using default
Memory Summary:
Program space used 89Ah ( 2202) of 8000h bytes ( 6.7%)
Data space used 229h ( 553) of E41h bytes ( 15.2%)
Configuration bits used 7h ( 7) of 7h words (100.0%)
EEPROM space used 0h ( 0) of 400h bytes ( 0.0%)
ID Location space used 8h ( 8) of 8h bytes (100.0%)
Data stack space used 0h ( 0) of B41h bytes ( 0.0%)
make[2]: Leaving directory 'C:/Users/acer kuttan/Documents/pk/pk/TokenOverWiFi' make[1]: Leaving directory 'C:/Users/acer kuttan/Documents/pk/pk/TokenOverWiFi'
BUILD SUCCESSFUL (total time: 9s) Loading code from C:/Users/acer kuttan/Documents/pk/pk/TokenOverWiFi/dist/default/production/TokenOverWiFi.production.hex... Loading completed
You are printing a string onto a pointer that is neither initialized or has allocated memory in the heap. From the looks of your logs, it would seem you're writing the program to an embedded system of some sort which probably doesn't have any memory protection via Virtual Memory and if it does, it is probably throwing a segmentation fault.
Declare ptr
as a character array of an arbitrary size. Given, your format string, I would say 30 would be sufficient but, you can set it to be higher if required
void ClientToServer(const char *client_msg)
{
int len = strlen(client_msg);
char ptr[30];
sprintf(ptr,"AT+CIPSEND=0,%d",len);
}
In your code, ptr is probably possessing some random value and it is writing your value to some address on your system, which may not even be mapped to your instruction/data memory (especially, NULL). I have also removed the str_len array which doesn't seem to be serving any purpose.