so I'm trying to iterate over a char array in c. The Array is defined in a header file like this.
char const SEQUENCE[] PROGMEM = "\MdTqZWYzVf5E661OAd4r7ylINBLNEAzO...
the array is well over 1 thousand characters in length.
next I've been told to use the extern key word to bring the array into my main.c file. this is how I've done that. Not sure if this is totally correct.
extern char const SEQUENCE[];
I've tried to loop over the array and pring each individual value like this.
for (uint8_t I = 0; I < 64; I ) { printf("%c ", SEQUENCE[I]); }
but I got this as an output.
␏ ␀ ␀ ␀ ␀ ␀ ␀ ..
I expected something like. M d T q ...
can anyone tell me what is going wrong here?
CodePudding user response:
As mentioned in comments, there are some immediately noticeable problems with what you have shown...
- type
uint8_t
is clearly one byte wide, yielding a maximum of 256 iterations ( 0 - 255 ) before its value begins to repeat back to zero, thus printing out the first 256 characters again, and again,.... This will not satisfy your stated buffer size of "well over 1 thousand characters". - The declaration
extern char const SEQUENCE[];
(I assume was to be in a header file.) although legal, is not usable for any discernible way within the stated objectives. It is an empty array, and must remain that way until the end of its life at program exit.
It is difficult to know for certain exactly what arrangement satisfies the requirements, because the requirements are not clearly stated mentioning only a header file declaration using extern
and const
to create a buffer, and that the buffer should then be brought out to be used in the main()
function. The following interpretation, although based on some assumptions does these things, and allows the buffer to be output:
in something.h
const char buf[] = "\MdTqZWYzVf5E661OAd4r7ylINBLNEAzO...";//smaller for illustration
#define PROGMEM sizeof(buf) //to be used when indexing through array
extern const char SEQUENCE[] = "\MdTqZWYzVf5E661OAd4r7ylINBLNEAzO...";
In main.c
#include "something.h"
int main(void)
{
for(int i = 0; i < PROGMEM; i )//note use of int
{
//extern usage
printf("%c", SEQUENCE[i]);
}
return 0;
}
Note: The extern
keyword is used to effect scope and duration, making a fuction/variable visible throughout a program. In this example, there is no compelling reason to use the extern
keyword other than it is a stated requirement. The buffer SEQUENCE
created in the header file without extern
scope is completely sufficient here. It may be interesting for you to review others thoughts on how and when the extern
keyword should be applied.
CodePudding user response:
header:
#ifndef INCLUDE_headername_H
#define INCLUDE_headername_H
...
extern const char* SEQUENCE;
...
#endif
c
#include "headername.h"
const char* SEQUENCE = "mystring..."
int main() {
for (int i = 0; SEQUENCE[i]; i ) {
...
}
}
This works, because SEQUENCE
is a string, and at the end (and only at the end) will be a null-terminater. This does not work, if you place one by yourself (with \0)