473,789 Members | 2,694 Online
Bytes | Software Development & Data Engineering Community
+ 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,passwor d,database,quer y);
}

(gdb) next
12 char *query[]={"SHOW DATABASES;"};
(gdb) next
13 db(user,passwor d,database,quer y);
(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 2134
gert <ge**********@g mail.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...@g mail.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...@g mail.comwrote:
On Jul 15, 6:47 pm, gert <gert.cuyk...@g mail.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;",NUL L};

Jul 15 '07 #5
gert <ge**********@g mail.comwrote:
On Jul 15, 6:47 pm, gert <gert.cuyk...@g mail.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**********@g mail.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**********@g mail.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.ukwro te:
Jens Thoms Toerring wrote, On 15/07/07 18:20:
gert <ge**********@g mail.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

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

Similar topics

8
2720
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 loop. ..... {
1
2961
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 problem: select * from dbo.VwTransaction where
2
4198
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+ characters in length, quite variable. If it matters, the TEXT may contain UNICODE characters... Example: CREATE TABLE a (id SERIAL, thetext TEXT); SELECT id FROM a WHERE thetext='Some other text'; One way I thought to optimize this process would...
5
6791
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
4950
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 COL1 : COL2(nvarchar) --------------------------- 100 345G01 200 123456789
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: System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---System.FormatException: Invalid length for a Base-64 char array. at
0
1354
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), Salary number(10,2),......) Now, there is an index on the Empno field. Now, inside a stored procedure, I am executing a statement select Name=Ename from Emp where Empno=@eno This @eno is varchar(25). So, SQL Server would do the implicit...
2
7042
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 carriage return as the delimiter. "empltable" is the table I am using, and "address" is the field I am looking to split. So far I have managed to:
1
2827
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 from multiple tables I setup 4 variables that I will populate at another time. These variables hold the data for database I want to connect to. Currently when I run the code, I end up with this as the output. test �y������� DB connection...
0
9663
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
9511
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
10195
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10136
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9016
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...
0
5415
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5548
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4090
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
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.