Computing.Net > Forums > Programming > Two questions about C (syntax)

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Two questions about C (syntax)

Reply to Message Icon

Name: whoami
Date: July 25, 2006 at 09:58:54 Pacific
OS: WinXP Pro
CPU/Ram: 2.4GHz/256RDRAM
Product: Gateway
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: July 25, 2006 at 10:46:57 Pacific
Reply:

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


0

Response Number 2
Name: whoami
Date: July 25, 2006 at 11:00:26 Pacific
Reply:

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


0

Response Number 3
Name: Mechanix2Go
Date: July 25, 2006 at 11:08:13 Pacific
Reply:

Sorry for the brain fart.

0 *IS* 30 hex.


If at first you don't succeed, you're about average.

M2


0

Response Number 4
Name: Guy
Date: July 27, 2006 at 15:20:09 Pacific
Reply:

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


0

Response Number 5
Name: whoami
Date: July 27, 2006 at 16:32:18 Pacific
Reply:

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.


0

Related Posts

See More



Response Number 6
Name: Guy
Date: July 27, 2006 at 21:01:55 Pacific
Reply:

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


0

Response Number 7
Name: bakss
Date: August 14, 2006 at 19:07:39 Pacific
Reply:

good deal. cp is hex number


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Two questions about C (syntax)

Simple design questions in C++ www.computing.net/answers/programming/simple-design-questions-in-c/4384.html

C++ Enumerations www.computing.net/answers/programming/c-enumerations/11254.html

Please reccomend a C learning book www.computing.net/answers/programming/please-reccomend-a-c-learning-book/7835.html