Solved

# defined memory location

Posted on 2003-11-03
4
248 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
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 …
Six Sigma Control Plans

726 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