473,396 Members | 1,866 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,396 software developers and data experts.

Testing pointers in while-loops

If you have the following string variable declared:

char *current;

What does the following test in a while-loop amount to:

While (!*current)

Does this check if *current is "not empty" i.e not null or does it
suggest something else.

cman

Mar 2 '07 #1
6 8608

"cman" <ti****@gmail.comwrote in message
news:11**********************@j27g2000cwj.googlegr oups.com...
If you have the following string variable declared:

char *current;

What does the following test in a while-loop amount to:

While (!*current)
Well, first is dereferences current, then tests what it pointed to against
0. This should promptly SEG-FAULT in your example though...

;^(
Mar 2 '07 #2
cman wrote:
If you have the following string variable declared:

char *current;
This is not a string variable. In fact C doesn't have a string type.
What does the following test in a while-loop amount to:

While (!*current)

Does this check if *current is "not empty" i.e not null or does it
suggest something else.
The statements within the while loop, if any, will be executed if
value yielded by deferencing current is zero.

Mar 2 '07 #3
On 2 Mar, 20:12, "cman" <til...@gmail.comwrote:
If you have the following string variable declared:

char *current;

What does the following test in a while-loop amount to:

While (!*current)

Does this check if *current is "not empty" i.e not null or does it
suggest something else.

cman
If you do something like:

char *current = "Hello";

then three things will happen. Firstly, the computer will reserve
enough space to store 6 characters (usually 6 bytes of memory).
Secondly, it will fill them up - the first five with the codes
representing the letters, and the sixth with 0 to show the end of the
string. And thirdly, it will set current to be the address of the
first one.

If you do:

while(!*current)

then this will continue as long as *current is 0. So if current is
pointing at (say) a letter, the loop will stop. But if current is
pointing at a 0, say the 0 at the end of a string, the loop will keep
going. It seems a bit of a strange thing to want to do but there may
be a use. For instance, you might do:

char current[20];

// read in a string into current here
while(!*current) {
// print message complaining that the user has
// typed in an empty string
// read in another string
}

Note that this is not the same as current being NULL. If current is
NULL this means it does not point at anything - not the begining of a
string, not the end of a string, nowhere.

Here's an example using a slightly different bit of code - note the
lack of a ! :

#include <stdio.h>

int main(void) {

char *current = "Test";

while(*current) {
printf("char %c\n", *current);
current++; }

return 0;
}

This prints out the characters of the string one at a time, and stops
when it hits the 0 at the end of the string.

Hope that helps.
Paul.

Mar 2 '07 #4
cman wrote:
>
If you have the following string variable declared:

char *current;
This is not a string variable. If you read what you wrote you
defined a pointer to a char.
>
What does the following test in a while-loop amount to:

While (!*current)
It may launch WWIII, polish your shoes, vomit in your lap, etc.
Dereferencing an uninitialized pointer invokes undefined
behaviour. You have also failed to define the function "While",
which is not the keyword "while".

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

Mar 2 '07 #5
Chris Thomasson wrote:
>
"cman" <ti****@gmail.comwrote in message
news:11**********************@j27g2000cwj.googlegr oups.com...
If you have the following string variable declared:

char *current;

What does the following test in a while-loop amount to:

While (!*current)

Well, first is dereferences current, then tests what it pointed to
against 0. This should promptly SEG-FAULT in your example though...
Should it? Why? There is a wide range of things that undefined behavior
can cause. There's no "should" about that.


Brian
Mar 3 '07 #6
"Default User" <de***********@yahoo.comwrote in message
news:54*************@mid.individual.net...
Chris Thomasson wrote:
>>
"cman" <ti****@gmail.comwrote in message
news:11**********************@j27g2000cwj.googleg roups.com...
If you have the following string variable declared:

char *current;

What does the following test in a while-loop amount to:

While (!*current)

Well, first is dereferences current, then tests what it pointed to
against 0. This should promptly SEG-FAULT in your example though...

Should it? Why? There is a wide range of things that undefined behavior
can cause. There's no "should" about that.
Great point! I stand corrected; thank you.
Mar 3 '07 #7

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

Similar topics

0
by: Jonathan Allen | last post by:
We have found that our method of testing does not match traditional text-book methodologies. I decided to write a short white paper on it so that I could get your opinions. Does anyone else use...
19
by: newbiecpp | last post by:
I am looking for a C++ unit testing program. I googled and found there are quite a few there, such as CppTest, C++Test, and DejaGnu. Can someone give me a recommendation among these programs? I...
388
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's...
19
by: s.subbarayan | last post by:
Dear all, I had this following doubt,while java is able to carryon with out pointers why C language cant be modified to remove pointer?Hows java able to do this with out pointers? I jus plead...
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
19
by: lihua | last post by:
Hi, Group! I got one question here: We all know that fclose() must be called after file operations to avoid unexpected errors.But there are really cases when you forget to do that!Just like...
72
by: Jacob | last post by:
I have compiled a set og unit testing recommendations based on my own experience on the concept. Feedback and suggestions for improvements are appreciated: ...
5
by: Mike Hofer | last post by:
I need some advice, folks, and I'm hoping you can provide it. First off, a few disclaimers: 1.) I am an ardant advocate of code quality. Especially, my own. 2.) I am maintaining a massive...
6
by: M Turo | last post by:
Hi, I was wondering if anyone can help. I'm want to pre-load a 'table' of function pointers that I can call using a its arrayed index, eg (non c code example) pFunc = func_A; pFunc = func_B;
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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...
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
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
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,...

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.