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

NULL reference

So I am in school learning how to program... we just did up a little program to count the number of characters in an array. I wrote it so that it would loop, counting as it goes, and end when it hits the NULL thingy....
It does not however end when it should... and with a printf() I display the contents of each element, and they are just random characters it seems.
Any help will be appreciated :)

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include <stdio.h>
  3.  
  4. char first[20];
  5. char *nameptr;
  6.  
  7. int counter=0;
  8.  
  9. int main()
  10. {
  11.  
  12.  
  13.     printf("Enter your first name: ");
  14.     scanf("%s",first);
  15.     nameptr=first;
  16.  
  17.     while(nameptr!=NULL)
  18.     {
  19.         nameptr++;
  20.         counter++;
  21.         printf("pointer %c\n",nameptr);
  22.         printf("counter %d\n",counter);
  23.     }
  24.  
  25.     printf("\n%d characters in your name",counter);
  26.  
  27.     return 0;
  28.  
  29. }
Apr 15 '10 #1

✓ answered by donbock

Please use CODE tags when you post source code. If you had then I could refer to line numbers in your source code.

nameptr points to successive characters within the string. You don't want to stop when the pointer value is NULL; you want to stop when the character being pointed to is null.

Notice that NULL is different from null!
  • NULL is a value that can be assigned to any pointer. Programs typically use this special pointer value to mean that the pointer variable doesn't point at anything.
  • Null (or null) is a character whose value is '\0'. By definition, all well-formed strings are terminated by null.

To look at the thing being pointed at by a pointer variable you have to dereference the pointer. This is accomplished by the unary asterisk (*) operator.

For example, your first printf statement in the loop uses the %c format string (print a single character) on the nameptr variable. You want to print the character being pointed at, not the pointer value itself. For example:
Expand|Select|Wrap|Line Numbers
  1. printf("%c", *nameptr);
Consider a bank with many safety deposit boxes in its vault. Each box has a unique number; and that same number is stamped on the corresponding key. The box number corresponds to a pointer value. The stuff inside the box corresponds to what is being pointed at.

2 1739
donbock
2,426 Expert 2GB
Please use CODE tags when you post source code. If you had then I could refer to line numbers in your source code.

nameptr points to successive characters within the string. You don't want to stop when the pointer value is NULL; you want to stop when the character being pointed to is null.

Notice that NULL is different from null!
  • NULL is a value that can be assigned to any pointer. Programs typically use this special pointer value to mean that the pointer variable doesn't point at anything.
  • Null (or null) is a character whose value is '\0'. By definition, all well-formed strings are terminated by null.

To look at the thing being pointed at by a pointer variable you have to dereference the pointer. This is accomplished by the unary asterisk (*) operator.

For example, your first printf statement in the loop uses the %c format string (print a single character) on the nameptr variable. You want to print the character being pointed at, not the pointer value itself. For example:
Expand|Select|Wrap|Line Numbers
  1. printf("%c", *nameptr);
Consider a bank with many safety deposit boxes in its vault. Each box has a unique number; and that same number is stamped on the corresponding key. The box number corresponds to a pointer value. The stuff inside the box corresponds to what is being pointed at.
Apr 16 '10 #2
That was great!
I learned more in the min that it took to read, than I did in 2 hours of class with my foreign speaking teach.
Very helpful, thank you!
Apr 16 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Pablo J Royo | last post by:
Hello: i have a function that reads a file as an argument and returns a reference to an object that contains some information obtained from the file: FData &ReadFile(string FilePath); But ,...
3
by: Marcus | last post by:
I'm running into a situation that has me adding a value of "Unknown" to a reference table. I am being pulled between two trains of thought, and was curious to get other's input on in. I give an...
15
by: JKop | last post by:
Does that Standard explicitly forbid the initiation of a null reference? Is there anything wrong with the following code?: void Blah( std::string const &k ) { if ( !&k ) return; // work with...
102
by: junky_fellow | last post by:
Can 0x0 be a valid virtual address in the address space of an application ? If it is valid, then the location pointed by a NULL pointer is also valid and application should not receive "SIGSEGV"...
6
by: RMD | last post by:
I need to be able to keep a list of object references, and null them out one by one at a later point in time. I realize this can be dangerous, but I have my reasons. I can't figure out, however,...
5
by: Boniek | last post by:
Hi I define a public property in a new form and I can see this property in table of Properties in Visual. How I can hide this property to see only in code ? Thank's Boniek
2
by: Fei Li | last post by:
Hi, Is null value for a class instance allowed in C#? for example: MyClass obj = null; If Not, how to initiate an instance with value 0? How to detect if an instance is "newed" ? And how to...
27
by: David W | last post by:
I'm almost tearing my hair out. A colleague claimed that a null reference can exist, like this: void f( int& p ) { printf( "%d\n", p ); } int main (int argc, char *argv) {
11
by: MikeT | last post by:
This may sound very elementary, but can you trap when your object is set to null within the object? I have created a class that registers an event from an object passed in the constructor. When...
76
by: valentin tihomirov | last post by:
As explained in "Using pointers vs. references" http://groups.google.ee/group/borland.public.delphi.objectpascal/browse_thread/thread/683c30f161fc1e9c/ab294c7b02e8faca#ab294c7b02e8faca , the...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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: 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
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
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,...

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.