473,395 Members | 1,584 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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)*strlen(SERVER_ROOT));
tmpstring = (char *)malloc(1000);
tmpstring = SERVER_ROOT;
//strcat (url, SERVER_ROOT);
filename = strcat(tmpstring, 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 2065
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)*strlen(SERVER_ROOT));
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(tmpstring, 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(tmpstring, "%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)*strlen(SERVER_ROOT));


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(tmpstring, 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(tmpstring, "%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)*strlen(SERVER_ROOT));
tmpstring = (char *)malloc(1000);
tmpstring[0] = "\0";
strcpy (tmpstring, SERVER_ROOT);
strcat (tmpstring, url);
//filename = strcat(tmpstring, url);

//sprintf(tmpstring,"%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
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...
20
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...
6
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. ...
3
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...
6
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: ...
7
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...
1
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'; ...
7
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...
13
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.