(08-26-2022, 09:37 PM)mnrvovrfc Wrote:(08-26-2022, 08:36 PM)TempodiBasic Wrote: never seen this way to assign a value to a string variable in CThat is a syntax error in C, perhaps not in C++. I've seen stranger things happen with C++ code, perhaps square-bracket operators were overloaded? Otherwise the intention of the programmer looks like one of the letters is being extracted.
buf[p][0]="CDEFGAB"[(n+700000)%7];
Just doing buf[p][0]="CDEFGAB"; might not be allowed in C, must allocate memory for pointer for string "array", then more memory for each member of the array and then use "strcpy()" or something like that from "string.h". This is just my near-fundamental knowledge of that programming language...
You got it. In C, string literals are one dimensional arrays of characters and can be treated as such.
The square brackets are an index into that array, allowing access to one character at a time.
It's not a kind thing to do to human source code reviewers, but it works.
Tricks like this are why C is often called a "write only" language. It can be playful fun writing C code like the above, but good luck understanding that source code later.
These two programs, the first in C and the second in Basic, print the same output, one character at a time:
Code: (Select All)
#include <stdio.h>
int main(void) {
int i=0;
char c;
for (i=0; i<6; ++i) {
c = "ABCDEF"[i];
putchar(c);
}
return 0;
}
Code: (Select All)
for i=1 to 6
c$ = mid$("ABCDEF",i,1)
print c$;
next