Solved

# defined memory location

Posted on 2003-11-03
4
243 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mapAB Challlenge 35 124
wordappend challenge 8 145
Windows Batch File - Display current time and date in FOR command 8 66
Turning python script into an applet 12 102
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

911 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

16 Experts available now in Live!

Get 1:1 Help Now