Turning array in main into a global, to later be altered by main?

  • A+

I have two arrays of the alphabet in the following format:

const char plain[26] = {'a','b',....'y','z'} // this is the the full alphabet const char crypt[26] = {'i','d',....'m','x'}; // this is the the alphabet scrambled 

The order of the alphabet in both arrays can change depending on input. This change happens in the main function.

The purpose of this is to match the letters of the string to the second, like an encryption. I compare characters to array values. So it would kind of look like this (simplified)

text[3] = 'yes'; changed[3]; if(text[0] == plain[25]){    //would be done under a for loop so 25 would be a changing integer value     changed[0] = [crypt[25]; } 

My code works under perfectly under the main function. I wanted to mention my purpose like this because I was having previous problems simply due to the type of array and formatting. And since the array is moved outside, I will probably/am running into those problems again.

Now I want to make the arrays global. The actual encryption happens in a function that does not take the arrays as a variable. But I want the function to have access to them.

Here's what it looks likes right now

const char plain[26]; const char crypt[26]; int maint(void){     const char plain[26] = {'a','b',....'y','z'} //values get changed here      const char crypt[26] = {'i','d',....'m','x'} //and here 

While this provides no errors, I dont get an output, I believe the other functions are using a blank array instead of the changed one (if the change even worked).

I've tried different array types, I believe the issue is in initialization or giving the array values.

Edit: To clarify, the two arrays can be in any order. A text file will randomize the order can give it to me in the format:

b,r m,o l,s ... ... ... 

Both cases the alphabet is randomized. Where the first column would correspond to the first array (plain), second would be to second array (crypt).

If theres a way to read by collums and store in the format

plain = 'bml...'; /whole alphabet randomized crypt = 'ros...'; /whole alphabet randomized

That would also work.


The plain and crypt you have in main aren't the same as the global ones. Since you declare them again, they're new ones that are only visible in main. Thus you're not changing the global ones.

Instead, only declare them once globally and do assignment in the main function:

char plain[26];  int main(void) {     memcpy(plain, "abcdefghijklmnopqrstuvwxyz", 26); //values get changed here     return 0; // indicate successful program execution } 

Also note that there are some syntax errors in

const char plain[26] = {'a','b',....'y','z'} //values get changed here  

Comments start with //, not //, and you need a ; after a statement. Also, the int main should return an int in C.

Of course, if you don't need to actually change the memory and only assign it to predefined sets of characters, you can do it like this:

const char *plain;  int main(void) {     plain = "abcdefghijklmnopqrstuvwxyz";     return 0; } 

This way you can still read from it with syntax like plain[5], but you can't assign to it with, say plain[5] = 'a';.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: