Page 1 of 1

String Manipulation

Posted: Fri Aug 26, 2022 5:56 pm
by PBandJacob
Hi, I'm not sure if I'm just using very bad syntax in C, but I'm getting junk data when I try to access strings element by element. If I assign an array of characters or a char* to a string value and print that out I have no issue, but if I try to access a string like "x[4]" I only get junk data, not the value in string x at offset 4.

I've tried:
char* x = "thisstringiswaytoolong";
printf(x[4]);

and

char x[] = {'t','h','i','s','i','s','w','a','y','t','o','o','l','o','n','g'};
printf(x[4]);

I get a junk value both times.
I'm able to use printf(x) to get the correct entry in the string.

Re: String Manipulation

Posted: Fri Aug 26, 2022 6:31 pm
by TomKerekes
printf expects its first parameter to be a pointer to char. You are passing a char. Also printf requires a new line character for it to print.

You can use the %c format specifer to print a single character.

Code: Select all

	char* x = "thisstringiswaytoolong";
	printf("%c\n",x[4]);
The TI Compiler is more strict on type checking. If you right-click and compile with it you get the error:

"C:\KMotion435f\C Programs\user6.c", line 6: error: argument of type "char" is incompatible with parameter of type "const char *"

Re: String Manipulation

Posted: Fri Aug 26, 2022 6:49 pm
by PBandJacob
Thanks, that worked. I feel silly for not noticing that, but thanks for the quick response.

Re: String Manipulation

Posted: Fri Aug 26, 2022 7:14 pm
by PBandJacob
Quick follow up (unless this should be a different thread):
If I assign a string to be too long: "thisstringiswaytoolong" and then I read in a new value for it using fscanf and the new value is shorter, I am still able to see the older part of the string with the correct array offset. Is this a memory leak? Or does the architecture/operating system of KMotion handle this in a way to prevent a memory leak?
i.e.
FILE *g = fopen("myfilepath");
char *x = "thisstringiswaytoolong"
fscanf(g, "%s", x);
printf(x); //X is fed in the string "shortstring\n"

printf(x) prints out "shortstring\n" (length 12) and printf("%c\n", x[14]); will print out the letter 'y' from the previous value originally stored in x.

Re: String Manipulation

Posted: Sat Aug 27, 2022 5:53 pm
by TomKerekes
"thisstringiswaytoolong" is considered a constant by the compiler and it is against the rules to modify it. The C Standard declares it to have undefined behavior. Some Compilers might put the string in read only memory. Also if this string (or part of it) is referenced elsewhere then the compiler might use it in both instances resulting in confusing results if you modify it.

KFLOP's Compiler doesn't make use of read only memory so it works. Global constants like this are allocated at Compile/Load time and don't result in memory leaks. Memory leaks occur when memory is dynamically requested at run time but never released. Dynamic memory allocation is not supported by KFLOP User Programs.

Note also if you were to read a longer string into the constant it would overwrite whatever constant that was placed in memory after it causing corruption.

The proper C technique is:

Code: Select all

char x[] = "thisstringiswaytoolong";
In this case the compiler creates a variable array of char called x big enough to hold the constant (plus a null terminator) and initializes the variable array from the constant array. You are then free to modify the variable.

HTH