strings

  • Thread starter Bill Cunningham
  • Start date
B

Bill Cunningham

I want to write a function named after simula's outtext. I want it to
behave exactly like printf does with strings. I have narrowed down a list of
what funtions I think I'll need but I'm not exactly sure how to put them
together. strlen is one isspace is another. What about alphanum ? Would it
take care of what I want? ie

outtext("hello world");

hello world

Thanks
 
D

Dann Corbit

Bill Cunningham said:
I want to write a function named after simula's outtext. I want it to
behave exactly like printf does with strings. I have narrowed down a list
of what funtions I think I'll need but I'm not exactly sure how to put
them together. strlen is one isspace is another. What about alphanum ?
Would it take care of what I want? ie

outtext("hello world");

hello world

You want:
puts("hello world");
 
B

Bill Cunningham

You want:
puts("hello world");

THanks for the wake up Dan. It would have to be that simple. Here's the
code I wrote though because puts was wanting a cast.

#include <stdio.h>

int outtext(char *buffer)
{
printf("%s",*buffer);
return 0;
}

Bill
 
D

Dann Corbit

Bill Cunningham said:
THanks for the wake up Dan. It would have to be that simple. Here's the
code I wrote though because puts was wanting a cast.

#include <stdio.h>

int outtext(char *buffer)
{
printf("%s",*buffer);

You don't want the contents of the pointer, but the pointer itself.
return 0;
}

e.g.:
#include <stdio.h>
int outtext(const char *buffer)
{
return puts(buffer);
}
 
B

Bill Cunningham

You don't want the contents of the pointer, but the pointer itself.


e.g.:
#include <stdio.h>
int outtext(const char *buffer)
{
return puts(buffer);
}
So *buffer is a pointer to the location of the string while buffer is
the pointer? I need to remember this from now on.
Bill
 
D

Dann Corbit

Bill Cunningham said:
So *buffer is a pointer to the location of the string while buffer is
the pointer? I need to remember this from now on.

In this context, *buffer is applying the indirection or dereferencing
operator to the string. In other words, you are going to get the first
character of the string. That is clearly not what you want to do.
 
K

Keith Thompson

Bill Cunningham said:
THanks for the wake up Dan. It would have to be that simple. Here's the
code I wrote though because puts was wanting a cast.

No, puts doesn't want a cast. If you're casting an argument to
puts(), you're almost certainly doing something wrong. Remove the
cast, read the error message, and find some other way to fix it.
#include <stdio.h>

int outtext(char *buffer)
{
printf("%s",*buffer);
return 0;
}

*buffer is of type char. printf with "%s" expects a char*. You're
not getting an error message because printf is a variadic function;
its prototype can't specify the types of all the arguments because
their types can vary depending on the format string (which isn't
necessarily a literal).

This:
printf("%s", buffer);
is correct; "%s" tells printf to expect a char*, and buffer is of type char*.

I suspect you tried something like this:
int outtext(char *buffer)
{
puts(*buffer);
return 0;
}
and got an error message. The solution is to change the puts call to:
puts(buffer);

Again, I think you're trying to use these functions without checking
their documentation. puts() doesn't expect a char, it expects a
char*. If you're not certain what it expects, you should look it up
before you write the call.

Also, note that
printf("%s", buffer);
and
puts(buffer);
are not equivalent. As your documentation will tell you, puts appends
a new-line to the string when it prints it. If you don't want the
new-line, you can use
fputs(buffer, stdout);
which *doesn't* append a new-line.

This difference in behavior between puts and fputs is, as far as I can
tell, entirely arbitrary, and it's one more demonstration of why you
should always read the documentation before trying to use any
function.
 
K

Keith Thompson

Bill Cunningham said:
So *buffer is a pointer to the location of the string while buffer is
the pointer? I need to remember this from now on.

No.

In general, unary "*" is the dereference operator. If p is a pointer,
then *p is what p points to.

Specifically, buffer is a pointer to char, so *buffer is a char. Not
a pointer of any kind, just a simple character value (likely an 8-bit
number).
 
B

Bill Cunningham

Keith Thompson said:
No, puts doesn't want a cast. If you're casting an argument to
puts(), you're almost certainly doing something wrong. Remove the
cast, read the error message, and find some other way to fix it.


*buffer is of type char. printf with "%s" expects a char*. You're
not getting an error message because printf is a variadic function;
its prototype can't specify the types of all the arguments because
their types can vary depending on the format string (which isn't
necessarily a literal).

This:
printf("%s", buffer);
is correct; "%s" tells printf to expect a char*, and buffer is of type
char*.

I suspect you tried something like this:
int outtext(char *buffer)
{
puts(*buffer);
return 0;
}
and got an error message. The solution is to change the puts call to:
puts(buffer);

Again, I think you're trying to use these functions without checking
their documentation. puts() doesn't expect a char, it expects a
char*. If you're not certain what it expects, you should look it up
before you write the call.

I did indeed read that puts takes a pointer to char and returns and int.
My problem here is lack of understanding of pointers. I did indeed write
puts(*buffer). Just looking at what I just wrote I would think puts is
taking a pointer.
It is not the same as puts(buffer). That's where I am goofing up.
 
B

Bill Cunningham

Keith Thompson said:
No.

In general, unary "*" is the dereference operator. If p is a pointer,
then *p is what p points to.

Specifically, buffer is a pointer to char, so *buffer is a char. Not
a pointer of any kind, just a simple character value (likely an 8-bit
number).
Ok is const char *buffer then pointing to the first char somewhere? Or
is the * in this case not a pointer at all but a "dereference operator". I'm
going to have to re-think everything here. No wonder I have so much trouble
reading other's source. When I see a * I think it's a pointer to a type. In
this case const char.
 
D

Dann Corbit

Bill Cunningham said:
Ok is const char *buffer then pointing to the first char somewhere? Or
is the * in this case not a pointer at all but a "dereference operator".
I'm going to have to re-think everything here. No wonder I have so much
trouble reading other's source. When I see a * I think it's a pointer to a
type. In this case const char.

You need to differentiate between a declaration and a dereference.

For instance, here is a declaration of a character pointer:

const char *p = "p is an address that point to this string";

and a dereference of the object pointed to by pointer p like this:

char c = *p; /* get the first char of p and store it in c */
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,056
Messages
2,570,441
Members
47,119
Latest member
NoeliaIrby

Latest Threads

Top