Solved

# defined memory location

Posted on 2003-11-03
4
242 Views
Last Modified: 2010-04-17
Hi, Can any of u tell me how i can define the memory location for the files that i create, using "# define" statement?? I have to create a list of files and i need to store them in a specified location. I have to speicfy that location using "# define" so that, if i want to change the storage location at one point, i just have to change the "# defined location". If u can explain with an example, it would be very nice of you.
0
Comment
Question by:motian
  • 2
4 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9670154
Hi motian,

AFAIK this is not possible in C
you cannot pre determine which memory location will be assigned to you by the compiler
all these abstractions are anyway handled by using variables.... why do you want to override compilers handling of memory ?

If you can specify what you are trying to achieve, we may be able to help you better

Cheers!
Sunny:o)
0
 

Author Comment

by:motian
ID: 9670260
Hi,
  Thank you for such a fast reply. I have made a mistake in my question. I meant the "hard disk storage" by saying, memory location.If i don't specify the disk in which my files should be stored,it goes to the default place of where my code resides.
 I don't want it that way but want to specify a desired disk(say D:\) while my code is in the C drive.And i should also be able to change it by just altering the "# define" statement. Hope you get my problem right,now.
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 30 total points
ID: 9670302
in that case, just #define the target drive/directory

#define LEAD_PATH D:\

whenever you wish to create/save a file add LEAD_PATH prefix to filename !!!
0
 

Expert Comment

by:kcy
ID: 9676124
I beg to differ slightly from sunnycoder,

If you try to put LEAD_PATH in quotes when you use it then you would not get the value it defines but the string "LEAD_PATH" instead.

I believe that this will work better:

#define LEAD_PATH "D:\\"   /* note: two slashes are necessary because a backslash is used for control characters in a string*/

and it would be used like this for instance:

fp = fopen(LEAD_PATH"morepath\\myfile.txt", "r");

This gets converted to by the preprocessor to:

fp = fopen("D:\\""morepath\\myfile.txt", "r");

and finally to:

fp = fopen("D:\\morepath\\myfile.txt", "r");

FYI: You CAN create a #define to access a memory location directly in C.  This is useful in embedded applications where you control the memory map.  It would look something like this:

#define MYMEM ((char*)(0x000F0000))   /* Where the location 0x000F0000 contains the start of the string "TEST" */

to use:

printf ("Third char is %c, Memory contains %s\n", MYMEM[2], MYMEM);

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now