Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello all. First off let me explain that I am completely new to programming. I just grabbed a Sam's Teach Yourself C in 21 Days book last week because I wanted to see if I can teach myself C, I am on day 11.
Below are my two questions:
1)
How would a C program execute if for instance you included a 0 (not meant to be NULL) character in a string?i.e.
#include <stdio.h>
#include <stdlib.h>
main(){
int i;
char *cp;cp = "a string";
while(*cp != 0)
{
putchar(*cp);
cp++;
}
putchar('\n');exit(EXIT_SUCCESS);
}The code above will successfully print the string "a string" as it detects the NULL character after the letter 'g'.
Now what will happen with the code below:
#include <stdio.h>
#include <stdlib.h>
main(){
int i;
char *cp;cp = "a string with a 0 ?";
while(*cp != 0)
{
putchar(*cp);
cp++;
}
putchar('\n');exit(EXIT_SUCCESS);
}I assume that only a portion of the string will be printed, i.e. "a string with a " up to the whitespace after the letter 'a'. Is this correct? How could I include the 0 character and still print it?
2)
A pointer declaration is the follwing:typename *ptrname;
I understand that ptrname points to a variable of type typename but is not a variable of typename itself. This is so that when doing pointer arithmetic the complier will know how to increment in memory depending on the required number of bytes of a variable typename (int is 2 B, char is 1 B, etc.)
My question is the pointer can be assigned an address of an array, starting address of an allocated block of memory, or any known address by the programmer. Does this imply that an address is not a variable? What I mean is that these addresses are numbers (whatever number base is not important) but they are not treated as int, float, double variables correct? So what exactly is happening when I initialize the pointer to some address? It is a number which is not a variable, therefore cannot be one of C's variables types right? Is it some "generic" number or maybe even some sort of "definition?"
Thanks for any help. I was thinking about these two things last night and will not have access to a compiler until late this evening so I cannot check myself, but the two questions are like thorns in my mind.
whoami

who,
I'm no programmer, but I compiled and ran the 1) and in both cases it puts out the whole string.
My guess is that:
while(*cp != 0)
means 0 hex, which is NOT the same as the char 0 [31 hex].
If at first you don't succeed, you're about average.M2

Thanks M2. Cool. It gives me something to look forward too. I may be wrong but I checked that the ASCII character 0 is 30 hex (48 dec). Dumb detail though.
However I glanced through another post about the differences between the NULL character and 0. I guess that I will now have to read that closely.
Anyone else for question 2???
whoami

Regarding 1), yeah:
'0' != '\0'
for sure.
Regarding 2) ........
your use of the exact term 'typename' bothered me - it is actually a reserved word in C++.
Address types - let's call them 'pointers'. They point at whatever you want them to, and arithmetic can be performed on them (although the rules of address arithmetic may not be what you would expect).
You seem to be absorbing the '21 days' info - I suggest you go to the library, get a copy of K&R's "The C Programming Language", and look at Chapter 5.
Keep working at it.
Guy

Guy:
Thanks. I'll try to check out that book.
Before I get a copy would you say that the address type is the same thing as a variable type? Meaning that whatever type of variable the pointer points to that pointer name/variable is of the same type as well? It does not seem correct to say that a pointer is of type char for example even though I think that computers store all variables/data in numeric form in memory but since you can do pointer arithmetic I must believe that the pointers should be of type int, float, double or some explicit numeric type. The address bus is a certain size so each memory address is a certain size which can be operated on.
I'll just exercise patience and get that book and search online. Thanks all.

A pointer (declared or cast) to type X is in some ways intimately "tied" to what type X is, and how it maps in to physical memory.
The compiler "takes care" of all this assuming variables are declared (or cast) to the proper type.
Given that a char usually occupies one byte of storage, and an int usually occupies 2 (or maybe 4), consider:
char * pc;
pc = ..... /* point to a char */
pc++; /* point to the next char in memory */In most architectures pc has been incremented by the number 1 (one).
int * pi;
pi = ..... /* point to an int */
pi++; /* point to the next int in memory */In most architectures pi has been incremented by the number 2 (or 4).
This concept applies even if dealing with e.g. a pointer to an array of structs.
If I have such, and do:
ps++;
ps is *actually* incremented by a number that is the length of the struct.
But is *still* points the the *next* such struct in memory.
Address busses ..... yeah ...... you may actually need to study some assembler. Then go back to C.
(Assembler is what *really* gives you a good gut feel about "pointers", as well as concepts concerning physical I/O, like bus width - learn the architecture at a "register" level).
Guy

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |