[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

# defined memory location

Posted on 2003-11-03
4
Medium Priority
?
253 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 120 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Make the most of your online learning experience.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Six Sigma Control Plans

656 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