473,486 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

char *query[]

Why does query[1] = www ?

int
main(void)
{
char *user="www";
char *password="";
char *database="www";
char *query[]={"SHOW DATABASES;"};
db(user,password,database,query);
}

(gdb) next
12 char *query[]={"SHOW DATABASES;"};
(gdb) next
13 db(user,password,database,query);
(gdb) print query
$5 = {0x400e25 "SHOW DATABASES;"}
(gdb) print query[0]
$6 = 0x400e25 "SHOW DATABASES;"
(gdb) print query[1]
$7 = 0x400e20 "www"
(gdb) print query[2]
$8 = 0x400e24 ""
(gdb) print query[3]
$9 = 0x400e20 "www"
(gdb) print query[4]
$10 = 0x0
(gdb)

source
----------
http://dfo.svn.sourceforge.net/viewv...74&view=markup

Jul 15 '07 #1
14 2102
gert <ge**********@gmail.comwrites:
Why does query[1] = www ?
Because you are accessing other strings outside of the scope of your
query definition.

Query is an arry of pointers to char with only one element.

query[1] (luckily) out of the bounds of query and happens to be same as
user which is pointing to "www".

(gdb) p query[1]
$4 = 0x804844c "www"
(gdb) p user
$5 = 0x804844c "www"
(gdb)

This is platform specific and you might as well have got a crash.
Jul 15 '07 #2
There goes my carefully thought out NASA for loop :(

for(q=0;s=query[q];q++)

Guess the only solution would be using a max value then ?
Jul 15 '07 #3
On Jul 15, 6:47 pm, gert <gert.cuyk...@gmail.comwrote:
There goes my carefully thought out NASA for loop :(

for(q=0;s=query[q];q++)

Guess the only solution would be using a max value then ?
o wait a minute what about

char **query = {"SHOW DATABASES;", 0x0 };
(gdb) print query[4]
$10 = 0x0
How can i force a 0x0 boundary ?

Jul 15 '07 #4
On Jul 15, 6:57 pm, gert <gert.cuyk...@gmail.comwrote:
On Jul 15, 6:47 pm, gert <gert.cuyk...@gmail.comwrote:
There goes my carefully thought out NASA for loop :(
for(q=0;s=query[q];q++)
Guess the only solution would be using a max value then ?

o wait a minute what about

char **query = {"SHOW DATABASES;", 0x0 };
(gdb) print query[4]
$10 = 0x0

How can i force a 0x0 boundary ?
victory, works like a charm :)

char *query[]={"SHOW DATABASES;",NULL};

Jul 15 '07 #5
gert <ge**********@gmail.comwrote:
On Jul 15, 6:47 pm, gert <gert.cuyk...@gmail.comwrote:
There goes my carefully thought out NASA for loop :(

for(q=0;s=query[q];q++)

Guess the only solution would be using a max value then ?
o wait a minute what about
char **query = {"SHOW DATABASES;", 0x0 };
You should use NULL instead of 0x0 - NULL is null pointer
while 0x0 is an integer constant (and the representation
of a null pointer isn't necessarily all bits 0 as in 0x0).

Now you can use your loop

for (q=0;s=query[q];q++ )

since s will be set to NULL for q==1 amd thus the loop will
end without accessing a non-existing array element.
(gdb) print query[4]
$10 = 0x0
How can i force a 0x0 boundary ?
I don't know what you mean with "0x0 boundary", but you
simply aren't allowed to ty to access elements past the
end of the array and you must find a way to avoid that,
like the one you already came up with, i.e. by having a
NULL pointer as the last element that you can test for.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Jul 15 '07 #6
On Sun, 15 Jul 2007 11:18:03 -0700, Barry Schwarz wrote:
The answer to your question at the start of your post is that you have
performed so much undefined behavior that you are no longer in the
realm of C. Any discussion of query[1] might as well be held in an
astrology newsgroup.
LOL...

--
Army1987 (Replace "NOSPAM" with "email")
"Never attribute to malice that which can be adequately explained
by stupidity." -- R. J. Hanlon (?)

Jul 15 '07 #7
Jens Thoms Toerring said:
gert <ge**********@gmail.comwrote:
<snip>
>char **query = {"SHOW DATABASES;", 0x0 };

You should use NULL instead of 0x0 - NULL is null pointer
while 0x0 is an integer constant (and the representation
of a null pointer isn't necessarily all bits 0 as in 0x0).
Whilst I agree that NULL is better, 0x0 will correctly initialise the
pointer to NULL even on platforms where all-bits-zero doesn't mean
NULL. The compiler is required to supply some magic if need be, to
ensure that p = 0 Does The Right Thing.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 15 '07 #8
Jens Thoms Toerring wrote, On 15/07/07 18:20:
gert <ge**********@gmail.comwrote:
<snip>
>char **query = {"SHOW DATABASES;", 0x0 };

You should use NULL instead of 0x0 - NULL is null pointer
while 0x0 is an integer constant (and the representation
of a null pointer isn't necessarily all bits 0 as in 0x0).
<snip>

Using NULL is better style, but an integer constant expression with a
value of 0 as shown by gert is also a valid null pointer constant and
even if null pointers are not all bits zero it will work.
--
Flash Gordon
Jul 15 '07 #9
Flash Gordon <sp**@flash-gordon.me.ukwrote:
Jens Thoms Toerring wrote, On 15/07/07 18:20:
gert <ge**********@gmail.comwrote:
<snip>
char **query = {"SHOW DATABASES;", 0x0 };
You should use NULL instead of 0x0 - NULL is null pointer
while 0x0 is an integer constant (and the representation
of a null pointer isn't necessarily all bits 0 as in 0x0).
<snip>
Using NULL is better style, but an integer constant expression with a
value of 0 as shown by gert is also a valid null pointer constant and
even if null pointers are not all bits zero it will work.
Sorry, of course - the 0 is here used where the compiler expects
a pointer, so it will automagically convert it to a NULL pointer.
I guess my brain is starting to boil after a temperature jump of
15 degree C here within 2 days...
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Jul 15 '07 #10
On Sun, 15 Jul 2007 16:57:56 -0000, gert <ge**********@gmail.com>
wrote:
>On Jul 15, 6:47 pm, gert <gert.cuyk...@gmail.comwrote:
>There goes my carefully thought out NASA for loop :(

for(q=0;s=query[q];q++)

Guess the only solution would be using a max value then ?

o wait a minute what about

char **query = {"SHOW DATABASES;", 0x0 };
Did you try this? Did you not get a compiler diagnostic complaining
about too many initializers?
>
>(gdb) print query[4]
$10 = 0x0

How can i force a 0x0 boundary ?
First tell us what a 0x0 boundary is. Do you mean where the address
is always a multiple of 0x10? If so, there is no way to do so
portably.
Remove del for email
Jul 16 '07 #11
On Jul 15, 9:20 pm, j...@toerring.de (Jens Thoms Toerring) wrote:
gert <gert.cuyk...@gmail.comwrote:
On Jul 15, 6:47 pm, gert <gert.cuyk...@gmail.comwrote:
There goes my carefully thought out NASA for loop :(
for(q=0;s=query[q];q++)
Guess the only solution would be using a max value then ?
o wait a minute what about
char **query = {"SHOW DATABASES;", 0x0 };

You should use NULL instead of 0x0 - NULL is null pointer
while 0x0 is an integer constant (and the representation
of a null pointer isn't necessarily all bits 0 as in 0x0).

Now you can use your loop

for (q=0;s=query[q];q++ )

since s will be set to NULL for q==1 amd thus the loop will
end without accessing a non-existing array element.
(gdb) print query[4]
$10 = 0x0
How can i force a 0x0 boundary ?

I don't know what you mean with "0x0 boundary", but you
simply aren't allowed to ty to access elements past the
end of the array and you must find a way to avoid that,
like the one you already came up with, i.e. by having a
NULL pointer as the last element that you can test for.

Regards, Jens
--
\ Jens Thoms Toerring ___ j...@toerring.de
\__________________________ http://toerring.de
Why not to use?
char *elem;
int i;
....
for(i=0; i < sizeof(query); i++) {
do_something_with(query[i]);
}
It is more straightforward and simple solution, I think.

crox

Jul 16 '07 #12
crox <cr*****@gmail.comwrites:
On Jul 15, 9:20 pm, j...@toerring.de (Jens Thoms Toerring) wrote:
>gert <gert.cuyk...@gmail.comwrote:
On Jul 15, 6:47 pm, gert <gert.cuyk...@gmail.comwrote:
There goes my carefully thought out NASA for loop :(
for(q=0;s=query[q];q++)
Guess the only solution would be using a max value then ?
o wait a minute what about
char **query = {"SHOW DATABASES;", 0x0 };
<snip>
>--
\ Jens Thoms Toerring ___ j...@toerring.de
\__________________________ http://toerring.de
Don't quote sigs.
Why not to use?
char *elem;
int i;
...
for(i=0; i < sizeof(query); i++) {
do_something_with(query[i]);
}
It is more straightforward and simple solution, I think.
Did you mean 'i < sizeof(query)/sizeof(*query)' perhaps? I am not sure
because of the mysterious 'char *elem;' declaration.

To the OP: if you go this route, take the terminating NULL (0x0) out
of the array and you must, of course, declare 'query' as an array:

char *query[] = { "one", "two" };

--
Ben.
Jul 16 '07 #13
On Jul 16, 3:16 pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
crox <craz...@gmail.comwrites:
On Jul 15, 9:20 pm, j...@toerring.de (Jens Thoms Toerring) wrote:
gert <gert.cuyk...@gmail.comwrote:
On Jul 15, 6:47 pm, gert <gert.cuyk...@gmail.comwrote:
There goes my carefully thought out NASA for loop :(
for(q=0;s=query[q];q++)
Guess the only solution would be using a max value then ?
o wait a minute what about
char **query = {"SHOW DATABASES;", 0x0 };
<snip>
--
\ Jens Thoms Toerring ___ j...@toerring.de
\__________________________ http://toerring.de

Don't quote sigs.
Why not to use?
char *elem;
int i;
...
for(i=0; i < sizeof(query); i++) {
do_something_with(query[i]);
}
It is more straightforward and simple solution, I think.

Did you mean 'i < sizeof(query)/sizeof(*query)' perhaps? I am not sure
because of the mysterious 'char *elem;' declaration.

To the OP: if you go this route, take the terminating NULL (0x0) out
of the array and you must, of course, declare 'query' as an array:

char *query[] = { "one", "two" };

--
Ben.
Oops, sorry I mean:
char *query[] = {"Do 1", "Do 2"};
int i;
...
for(i=0; i < sizeof(query); i++) {
do_something_with(query[i]);
}

Jul 17 '07 #14
crox wrote:
On Jul 16, 3:16 pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
crox <craz...@gmail.comwrites:
On Jul 15, 9:20 pm, j...@toerring.de (Jens Thoms Toerring) wrote:
>gert <gert.cuyk...@gmail.comwrote:
On Jul 15, 6:47 pm, gert <gert.cuyk...@gmail.comwrote:
There goes my carefully thought out NASA for loop :(
for(q=0;s=query[q];q++)
Guess the only solution would be using a max value then ?
o wait a minute what about
char **query = {"SHOW DATABASES;", 0x0 };
<snip>
>--
> \ Jens Thoms Toerring ___ j...@toerring.de
> \__________________________ http://toerring.de
Don't quote sigs.
Why not to use?
char *elem;
int i;
...
for(i=0; i < sizeof(query); i++) {
do_something_with(query[i]);
}
It is more straightforward and simple solution, I think.
Did you mean 'i < sizeof(query)/sizeof(*query)' perhaps? I am not sure
because of the mysterious 'char *elem;' declaration.

To the OP: if you go this route, take the terminating NULL (0x0) out
of the array and you must, of course, declare 'query' as an array:

char *query[] = { "one", "two" };

--
Ben.
When will you do us the favour of not quoting sigs?
Oops, sorry I mean:
char *query[] = {"Do 1", "Do 2"};
int i;
...
for(i=0; i < sizeof(query); i++) {
do_something_with(query[i]);
}
This is wrong. sizeof(query) will return the size, in bytes of the
array named query. You want the number of elements in the array. For
that, you should do:

sizeof query / sizeof query[0];

This will yield two.

Jul 17 '07 #15

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

Similar topics

8
2700
by: david | last post by:
Hello, Is the code below correct ? I always get an seg fault on execution and the debugger points to c_str() ? Basically i want to convert the value std::string to char * within the iterator...
1
2928
by: Amir | last post by:
Hi all, I have a table called PTRANS with few columns (see create script below). I have created a view on top that this table VwTransaction (See below) I can now run this query without a...
2
4156
by: Jon Lapham | last post by:
I have a table that stores TEXT information. I need query this table to find *exact* matches to the TEXT... no regular expressions, no LIKE queries, etc. The TEXT could be from 1 to 10000+...
5
6761
by: tienlx | last post by:
Hi all, I'm have a problem that i don't know how to convert object to char in C# . Does anybody know ? Thanks.
7
4936
by: jaawaad | last post by:
I have a text field in a table that contains number along with chars. Is there a way i can write a query to show all the fields that contains just Numbers or Char in a field?? TBALE Example ...
2
2217
by: Wayne Sepega | last post by:
I'm receiving the above error from time to time with one of our web pages. What would cause this? Thanks Wayne The following is the stacktrace from the exception: ...
0
1340
by: Medhatithi | last post by:
I have a table whose indexed column is a char(16) field. I am giving the schema. CREATE TABLE Emp(Empno char(16), Ename varchar(25), ...
2
7015
by: iritchie | last post by:
*(Apologies, I posted this in the SQL Server forum first) Hello all, I am trying to write a query which breaks down a single address field into individual fields, with char(10) or a...
1
2799
by: rottmanj | last post by:
I have written a class that manages all the database connection information/methods I need to connect to a database and query off of it. In order to make my application dynamic so that it can read...
0
7100
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,...
0
7126
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
7175
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...
1
6842
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...
0
7330
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4559
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...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
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 ...
0
262
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...

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.