473,795 Members | 3,481 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

creating a variable from constant

Hi, I am kinda new to c and i've encountered a problem with
program because i need to change a constant. I ofcourse don't
want to change a constant so is there any way to convert a
constant to a variable? I've tried something with malloc but
it didn't work for me. Here's what i tried:

char *url;
char *filename;
char *tmpstring;
url = strtok(header, " ");
url = strtok(NULL, " ");
puts (url);
//tmpstring = (char *)malloc(sizeof (SERVER_ROOT)*s trlen(SERVER_RO OT));
tmpstring = (char *)malloc(1000);
tmpstring = SERVER_ROOT;
//strcat (url, SERVER_ROOT);
filename = strcat(tmpstrin g, url);
puts (url);

as you can see i am trieing to combine SERVER_ROOT and url to
get something like /var/www/html/news.html where SERVER_ROOT
is /var/www/html and url = /news.html

Can anyone please help me?
Thanks in advance,

Robert
Nov 13 '05 #1
3 2090
Robert <R.****@hetnet. nl> writes:
Hi, I am kinda new to c and i've encountered a problem with
program because i need to change a constant. I ofcourse don't
want to change a constant so is there any way to convert a
constant to a variable? I've tried something with malloc but
it didn't work for me. Here's what i tried:

char *url;
char *filename;
char *tmpstring;
url = strtok(header, " ");
url = strtok(NULL, " ");
strtok() has at least these problems:

* It merges adjacent delimiters. If you use a comma as
your delimiter, then "a,,b,c" is three tokens, not
four. This is often the wrong thing to do. In fact,
it is only the right thing to do, in my experience,
when the delimiter set is limited to white space.

* The identity of the delimiter is lost, because it is
changed to a null terminator.

* It modifies the string that it tokenizes. This is bad
because it forces you to make a copy of the string if
you want to use it later. It also means that you can't
tokenize a string literal with it; this is not
necessarily something you'd want to do all the time but
it is surprising.

* It can only be used once at a time. If a sequence of
strtok() calls is ongoing and another one is started,
the state of the first one is lost. This isn't a
problem for small programs but it is easy to lose track
of such things in hierarchies of nested functions in
large programs. In other words, strtok() breaks
encapsulation.
puts (url);
//tmpstring = (char *)malloc(sizeof (SERVER_ROOT)*s trlen(SERVER_RO OT));
When calling malloc(), I recommend using the sizeof operator on
the object you are allocating, not on the type. For instance,
*don't* write this:

int *x = malloc (sizeof (int) * 128); /* Don't do this! */

Instead, write it this way:

int *x = malloc (sizeof *x * 128);

There's a few reasons to do it this way:

* If you ever change the type that `x' points to, it's not
necessary to change the malloc() call as well.

This is more of a problem in a large program, but it's still
convenient in a small one.

* Taking the size of an object makes writing the statement
less error-prone. You can verify that the sizeof syntax is
correct without having to look at the declaration.
I don't recommend casting the return value of malloc():

* The cast is not required in ANSI C.

* Casting its return value can mask a failure to #include
<stdlib.h>, which leads to undefined behavior.

* If you cast to the wrong type by accident, odd failures can
result.

Also, I don't understand why you'd multiply those two values.
What is SERVER_ROOT?
tmpstring = (char *)malloc(1000);
tmpstring = SERVER_ROOT;
Here's a real problem. You malloc() 1000 bytes of memory, and
then you just go ahead and throw it away. You probably want to
use strcpy() instead of assignment in the second statement there.
//strcat (url, SERVER_ROOT);
strcat() only works on strings, and before you put any data into
malloc()'d memory, it's not a string. You could use strcat() if
you first assigned '\0' to the first byte malloc()'d,
e.g. tmpstring[0] = '\0';
filename = strcat(tmpstrin g, url);
puts (url);

as you can see i am trieing to combine SERVER_ROOT and url to
get something like /var/www/html/news.html where SERVER_ROOT
is /var/www/html and url = /news.html


You can do that in a single function call with sprintf():
sprintf(tmpstri ng, "%s%s", SERVER_ROOT, url);
--
"I don't have C&V for that handy, but I've got Dan Pop."
--E. Gibbons
Nov 13 '05 #2
Ben Pfaff wrote:
Robert <R.****@hetnet. nl> writes:
Hi, I am kinda new to c and i've encountered a problem with
program because i need to change a constant. I ofcourse don't
want to change a constant so is there any way to convert a
constant to a variable? I've tried something with malloc but
it didn't work for me. Here's what i tried:

char *url;
char *filename;
char *tmpstring;
url = strtok(header, " ");
url = strtok(NULL, " ");


strtok() has at least these problems:

* It merges adjacent delimiters. If you use a comma as
your delimiter, then "a,,b,c" is three tokens, not
four. This is often the wrong thing to do. In fact,
it is only the right thing to do, in my experience,
when the delimiter set is limited to white space.

* The identity of the delimiter is lost, because it is
changed to a null terminator.

* It modifies the string that it tokenizes. This is bad
because it forces you to make a copy of the string if
you want to use it later. It also means that you can't
tokenize a string literal with it; this is not
necessarily something you'd want to do all the time but
it is surprising.

* It can only be used once at a time. If a sequence of
strtok() calls is ongoing and another one is started,
the state of the first one is lost. This isn't a
problem for small programs but it is easy to lose track
of such things in hierarchies of nested functions in
large programs. In other words, strtok() breaks
encapsulation.
puts (url);
//tmpstring = (char *)malloc(sizeof (SERVER_ROOT)*s trlen(SERVER_RO OT));


When calling malloc(), I recommend using the sizeof operator on
the object you are allocating, not on the type. For instance,
*don't* write this:

int *x = malloc (sizeof (int) * 128); /* Don't do this! */

Instead, write it this way:

int *x = malloc (sizeof *x * 128);

There's a few reasons to do it this way:

* If you ever change the type that `x' points to, it's not
necessary to change the malloc() call as well.

This is more of a problem in a large program, but it's still
convenient in a small one.

* Taking the size of an object makes writing the statement
less error-prone. You can verify that the sizeof syntax is
correct without having to look at the declaration.
I don't recommend casting the return value of malloc():

* The cast is not required in ANSI C.

* Casting its return value can mask a failure to #include
<stdlib.h>, which leads to undefined behavior.

* If you cast to the wrong type by accident, odd failures can
result.

Also, I don't understand why you'd multiply those two values.
What is SERVER_ROOT?
tmpstring = (char *)malloc(1000);
tmpstring = SERVER_ROOT;


Here's a real problem. You malloc() 1000 bytes of memory, and
then you just go ahead and throw it away. You probably want to
use strcpy() instead of assignment in the second statement there.
//strcat (url, SERVER_ROOT);


strcat() only works on strings, and before you put any data into
malloc()'d memory, it's not a string. You could use strcat() if
you first assigned '\0' to the first byte malloc()'d,
e.g. tmpstring[0] = '\0';
filename = strcat(tmpstrin g, url);
puts (url);

as you can see i am trieing to combine SERVER_ROOT and url to
get something like /var/www/html/news.html where SERVER_ROOT
is /var/www/html and url = /news.html


You can do that in a single function call with sprintf():
sprintf(tmpstri ng, "%s%s", SERVER_ROOT, url);


Thanks, i got it working now after a few experiments.
It's now something like:

char *url;
char *filename;
char *tmpstring;
url = strtok(header, " ");
url = strtok(NULL, " ");
puts (url);
//tmpstring = (char *)malloc(sizeof (SERVER_ROOT)*s trlen(SERVER_RO OT));
tmpstring = (char *)malloc(1000);
tmpstring[0] = "\0";
strcpy (tmpstring, SERVER_ROOT);
strcat (tmpstring, url);
//filename = strcat(tmpstrin g, url);

//sprintf(tmpstri ng,"%s%u", SERVER_ROOT, url);

puts (tmpstring);

btw, as you can see i was to lazy to change the malloc really
before i posted this, i am going to change that ofcourse.

Once again thanks.
Robert
Nov 13 '05 #3
Robert <R.****@hetnet. nl> writes:
tmpstring = (char *)malloc(1000);
tmpstring[0] = "\0";
strcpy (tmpstring, SERVER_ROOT);
strcat (tmpstring, url);


If you use strcpy(), you don't need to initialize tmpstring[0].
strcpy() doesn't assume that its destination is a string.
--
"I don't have C&V for that handy, but I've got Dan Pop."
--E. Gibbons
Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3950
by: nielson | last post by:
I have a class which contains (1) a class variable and (2) a method (e.g., methodA) which returns a reference to the class variable. If the variable has been assigned a value by a constant initializer in the "var" statement, my httpd process crashes with a segmentation fault when I call methodA. If the variable is assigned a value in one of the class methods, the httpd process does not crash when methodA is called and the value returned...
20
2108
by: CoolPint | last post by:
While I was reading about const_cast, I got curious and wanted to know if I could modify a constant variable through a pointer which has been "const_cast"ed. Since the pointer would be pointing to the constant variable and if I changed the value through the pointer, the constant variable should have been modified. Well, that's what I thought until I wrote the following and run it. #include <iostream> using std::cout; using std::endl;
6
2276
by: Joe Molloy | last post by:
Hi, I'm wondering is there any way I can get a variable's value from within a class when the variable has been declared outside the class but in the same script as the class is contained in. For example, say I have the following script <?php constant myvar = "my original string";
3
516
by: Robert | last post by:
Hi, I am kinda new to c and i've encountered a problem with program because i need to change a constant. I ofcourse don't want to change a constant so is there any way to convert a constant to a variable? I've tried something with malloc but it didn't work for me. Here's what i tried: char *url; char *filename; char *tmpstring; url = strtok(header, " ");
6
3914
by: Michael B Allen | last post by:
I want to initialize a static variable to a "random" value like: static void * get_key(struct dnsreq *req) { static uint16_t next_txnid = (uint32_t)req & 0xFFFF; But gcc gives me an error: src/dns.c: In function `get_key':
7
3171
by: icosahedron | last post by:
Is there a way to determine if a parameter to a function is a constant (e.g. 2.0f) versus a variable? Is there some way to determine if this is the case? (Say some metaprogramming tip or type trait?) I have a function with an if statement, that I would like to optimize away somehow. I was hoping the compiler would do it for me, but it doesn't seem to. Jay
1
14828
by: Peter | last post by:
Hi, I know that it is possible to build up a variable name based on a string and another variable name i.e. $varnum=5; ${"varnumber_$varnum"} = 'This variable should be called varnumber_5'; but is it possible to do this for a constant? For example I may have constants set up as
7
2178
by: Kristian Virkus | last post by:
Hello! I would like to write a C programm with multiple include files, where one may use another one (to print the value of a variable). When I try to compile the program below, the linker says, there's a multiple definition of 'value'. I'm using Windows XP, MinGW (included with Code::Blocks 1.0RC2) with GCC 3.4.4. Can anyone help me to solve the problem?
13
1868
by: bobg.hahc | last post by:
running access 2k; And before anything else is said - "Yes, Virginia, I know you can NOT use a variable to set a constant (that's why it's constant)". BUT - my problem is - I want a constant, that I can set from a variable (one time)!!! When my application starts, the user is prompted to make a selection from a list. I want that selection to go into a global variable, and NEVER CHANGE
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9042
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3723
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.