Follow this Blog!

Hi Everyone, f Comment

I am Michael . I blog about helpful tips, anything from technical (e.g. how to remove computer virus ) to hobbies (e.g. how to solve the rubik's cube ) to my favorite crafts like how to make an origami swan that may help you solve an issue you've long had or simply inspired you to enjoy life more!

Google has been a great friend to me for helping me find solutions to my problems, but it is not omnipotent. Quite often I just cannot find the solution regardless of how I tweak the search query. It is frustrating and disappointing.

When that happens it doesn't mean that the solution does not exist. It may mean that it exists but for some reason you cannot find it on Google or any other search engine.

To that end I try to make every one of my blogs search engine friendly so that you have the best chance of finding useful information. This is the purpose behind!

Feel free to leave a comment in the comment box below each post!

Check below for my latest posts!

Sep 22, 2016

C Programming Language: How to Create a Global Char Array with Another Variable?
Amazon If you use C, a need may arise where you want to create a global char array from other variables. The following is sample code showing how you normally create a global string variables:
char fileName[]="D:\\image\\original.bmp";
char fileNameFace[]="D:\\image\\face.bmp";

void someFunction(){
// use fileName
// use fileNameFace
As you can see, someFunction can use fileName and fileNameFace because fileName and fileNameFace are global variables.

However, suppose you want to change the folder from image to anotherImageFolder, you will need to manually change the value of fileName and that of fileNameFace. While doing so may not be much of a hassle, consider the case where you have 10 such strings. Changing 10 string variables can be a hassle.

So question is: How can you extract the folder name and make it a variable? If you can do that, you will only need to change the value of one variable instead of 10.

Declare a macro
#define createString(s1, s2, s3) sprintf(s1, "D:\\%s\\%s.bmp", s2, s3);
This macro allows you to construct a string, aka a char array, from two other strings.

Declare the strings you need

In my example, I need the following strings. Declare them at the top of your source code below the macro. Declare more if you need more.
char imageDir[]="image";
char fileName[1024];
char fileNameFace[1024];
The char array variable imageDir is for you to put the name of the image folder.

Create strings in the beginning of main()

In the beginning of your main() function, write the following code:
createString(fileName, imageDir, "original");
createString(fileNameFace, imageDir, "face");
That's it! Next time suppose you want to change the folder from image to bigImage, simply manually change the following line of code:

char imageDir[]="image";


char imageDir[]="bigImage";

And recompile and run the program and fileName, fileNameFace, and all other variables you may have declared will be updated with the new folder automatically. One manual change is all you need to update all the related char array variables. How nice!

Questions? Let me know!
Please leave a comment here!
One Minute Information - by Michael Wen
Find Michael on Google or Facebook
ADVERTISING WITH US - Direct your advertising requests to Michael