integer to characters

G

Gene Wirchenko

Gene said:
On Wed, 22 Oct 2003 22:38:24 GMT, Martin Ambuhl

[snip]
int main(void)
{
int x[3] = { 123, 23, 3 };
char buf[BUFSIZ], *p;
size_t i;
for (i = 0; i < sizeof x / sizeof *x; i++) {
^^^^^^^^^
What does this mean? I can not help but read it as the size of
an int * *. I would have written "sizeof(int)".

How would you get a pointer to pointer from applying the dereferencing
operator to a pointer?

I was looking at an asterisk. You can easily get a pointer with
an asterisk. I have never seen sizeof used like that before.
Yes, that divides the size of an entire array but the size of one
element, giving the number of elements in the array. Only works with
real arrays.

I know that part of it.

Sincerely,

Gene Wirchenko
 
A

Alan

I want to search a string in a file, but I don't know the position, how can
I search the string and stop at that line containing that string ?

for example, I want to seach the string ".ABC" in a file, how to do ?
thx
 
J

Jerry Coffin

I want to search a string in a file, but I don't know the position, how can
I search the string and stop at that line containing that string ?

for example, I want to seach the string ".ABC" in a file, how to do ?

Read data from the file into a string. Use string::find (for one
example) to find whether the string contains ".ABC", and repeat until
you find the string or the end of the file.

If you're concerned with speed, you might want to read about string
searching algorithms -- the Boyer-Moore-Horspool is generally thought of
quite highly. For truly maximum speed, you'll probably end up with
various platform-specific tricks for file reading (in my experience, you
can usually search right at the maximum transfer speed of the hardware,
but not with portable code).
 
R

Richard Heathfield

Jerry said:
Read data from the file into a string. Use string::find (for one
example) to find whether the string contains ".ABC", and repeat until
you find the string or the end of the file.

That doesn't work in C. See thread title.

Followups set.
 
R

Rick

Stefan said:
this fails the OPs spec.

Why not use the standard library for things it was
made for?

I know this is off-topic but I'm used to working with a compiler which
does not support sprintf or standard C string formatting commands
(AVR-GCC). The newer versions support these functions but since I'm
working on an older version, sprintf is not available to me. That's
probably why I gave that solution.


Rick
 
R

Richard Heathfield

[Followups set to acllcc++]
I want to search a string in a file, but I don't know the position, how
can I search the string and stop at that line containing that string ?

for example, I want to seach the string ".ABC" in a file, how to do ?

Read the string one line at a time. If you know the longest line you can
expect, then fgets is suitable for this. If you don't, consider using
something along the same lines as my fgetline function, which you can get
from:

http://users.powernet.co.uk/eton/c/fgetline.h
http://users.powernet.co.uk/eton/c/fgetline.c

Using my code, the problem is easily solved:

#include <stdio.h>
#include <string.h>

#include "fgetline.h"

int main(int argc, char **argv)
{
if(argc > 2)
{
FILE *fp = fopen(argv[1], "r");
if(fp != NULL)
{
char *line = NULL;
size_t size = 0;
while(0 == fgetline(&line, &size, (size_t)-1, fp, 0))
{
if(strstr(line, argv[2]) != NULL)
{
printf("%s\n", line);
}
}
free(line);
fclose(fp);
}
else
{
handle the file-opening error correctly
}
}
else
{
explain to the user how to use the program
}

return 0;
}


If you'd rather use fgets:

#include <stdio.h>
#include <string.h>

#define MAXLINE 1024 /* change this if necessary */

int main(int argc, char **argv)
{
if(argc > 2)
{
FILE *fp = fopen(argv[1], "r");
if(fp != NULL)
{
char line[MAXLINE + 2] = {0};
while(fgets(line, sizeof line, fp) != NULL)
{
if(strstr(line, argv[2]) != NULL)
{
printf("%s", line);
}
}
fclose(fp);
}
else
{
handle the file-opening error correctly
}
}
else
{
explain to the user how to use the program
}

return 0;
}
 
?

=?iso-8859-1?Q?Andr=E9_P=F6nitz?=

In alt.comp.lang.learn.c-c++ Gene Wirchenko said:
On Wed, 22 Oct 2003 22:38:24 GMT, Martin Ambuhl

[snip]
int main(void)
{
int x[3] = { 123, 23, 3 };
char buf[BUFSIZ], *p;
size_t i;
for (i = 0; i < sizeof x / sizeof *x; i++) {
^^^^^^^^^
What does this mean? I can not help but read it as the size of
an int * *. I would have written "sizeof(int)".

It's the same as sizeof x[0]. ( *(x + p) == x[p] )

The benefit is its independence of the array type. I.e. the expression
won't break if you replace 'char' by 'int'.

Andre'
 
D

Default User

Gene said:
On Wed, 22 Oct 2003 23:40:52 GMT, Default User


I was looking at an asterisk. You can easily get a pointer with
an asterisk. I have never seen sizeof used like that before.

Only in a declaration. Is that a declaration?



Brian Rodenborn
 
G

Gene Wirchenko

Only in a declaration. Is that a declaration?

I did not know what was wrong with my understanding, only that
something was. I tried to figure it out, but did not succeed. That
is why I posted my question.

BTW, here is a statement where * does get you a pointer, and it
is not in a declaration>:
int i;
int * * ppPedantic=&&i;
int * pPedantic;
pPedantic=*ppPedantic; // Voila: a pointer from *.
<BEG>

Yes, I know: not your intent. If you--presumably knowing C/C++
better than I--can miss something like this in your answer though,
think of the confusions I can have! (I am getting better.)

Sincerely,

Gene Wirchenko
 
J

Jerry Coffin

[ asterisks and pointers ]

In a declaration/definition, an asterisk denotes a level of reference or
indirection. I.e. for any type "X", "X *y" means that y is a pointer to
an X. If X happens to be a pointer, then y is a pointer to a pointer.
Theoretically, this can be carried out indefinitely -- e.g. if you have
a situation that calls for 32 levels deep of pointer to pointer to
pointer to pointer ... to whatever, you can do that (realistically, of
course, any compiler has some limit on levels of indirection, but the
practical limit is normally MUCH lower).

Elsewhere, an asterisk (ignoring multiplication for the moment) denotes
a level of dereference. I.e. if y is a pointer to some type X, then *y
denotes an X. Again, if the type X happens to itself be a pointer, then
*y will be a pointer. This can also be arbitrarily deep, so you could
have something like:

*(x->y[1]).z->a->b.c

Note that "x->y" is equivalent to "(*x).y" and "x[y]" is equivalent to
"*(x+y)", so even if it doesn't look like it initially, this is really a
series of applications of the unary "*" operator (interspersed with a
few applications of "." and "+").
 
D

Default User

Gene said:
I did not know what was wrong with my understanding, only that
something was. I tried to figure it out, but did not succeed. That
is why I posted my question.

It was less of a question that a refutation.
BTW, here is a statement where * does get you a pointer, and it
is not in a declaration>:
int i;
int * * ppPedantic=&&i;
int * pPedantic;
pPedantic=*ppPedantic; // Voila: a pointer from *.
<BEG>

This is a different problem, of course.
Yes, I know: not your intent. If you--presumably knowing C/C++
better than I--can miss something like this in your answer though,
think of the confusions I can have! (I am getting better.)

I didn't miss anything, I deliberately chose not to mention it because
things were confused enough.




Brian Rodenborn
 
G

Gene Wirchenko

It was less of a question that a refutation.

No, it was definitely a question.

Per dictionary.com, refute: "1. To prove to be false or
erroneous; overthrow by argument or proof: refute testimony. 2. To
deny the accuracy or truth of: refuted the results of the poll."

I did neither.

[snip]

Sincerely,

Gene Wirchenko
 
C

CBFalconer

Jerry said:
(e-mail address removed) says...
.... snip ...

Read data from the file into a string. Use string::find (for
one example) to find whether the string contains ".ABC", and
repeat until you find the string or the end of the file.

Please do not give C++ solutions in c.l.c.
 
C

CBFalconer

Gene said:
.... snip ...

BTW, here is a statement where * does get you a pointer, and it
is not in a declaration>:
int i;
int * * ppPedantic=&&i;
int * pPedantic;
pPedantic=*ppPedantic; // Voila: a pointer from *.

What in the devil is &&i ?. The address of i does not have an
address. A pointer may have an address. You cannot take the
address of an expression. You could write:

int i;
int *pPedantic = &i;
int * *ppPedantic = &pPedantic;
pPedantic = *ppPedantic; /* which reminds me of */
/* a cat chasing its tail */
 
G

Gene Wirchenko

What in the devil is &&i ?. The address of i does not have an

I had a feeling that that was going to get me in trouble. I
wanted a short example, and it was a moment of weakness. I can not
blame it on Friday (though that might be useful for this post).
address. A pointer may have an address. You cannot take the
address of an expression. You could write:

int i;
int *pPedantic = &i;
int * *ppPedantic = &pPedantic;
pPedantic = *ppPedantic; /* which reminds me of */
/* a cat chasing its tail */

Cats are pedantic, too?

I have just had to deal with pointer-to-pointer in a program.
Since where I work is strong on smart pointers, I tried to convert it
from unsafe to safe. I could not come up with anything. Hey, maybe I
should post it.

This is the unsafe pointer line:
char * * lpp_addresses=lp_hostinfo->h_addr_list;
I am using Winsock 1.1. gethostbyname() returns a pointer to a
HOST_ENT structure in its memory area. (In the above, lp_hostinfo
holds the pointer value.) One of the structure members is h_addr_list
which is char * *. If I wish to walk the pointers and pick up all of
the addresses, how do I declare the smart pointer version of
lpp_addresses?

Sincerely,

Gene Wirchenko
 
B

B. v Ingen Schenau

Gene said:
I have just had to deal with pointer-to-pointer in a program.
Since where I work is strong on smart pointers, I tried to convert it
from unsafe to safe. I could not come up with anything. Hey, maybe I
should post it.

This is the unsafe pointer line:
char * * lpp_addresses=lp_hostinfo->h_addr_list;
I am using Winsock 1.1. gethostbyname() returns a pointer to a
HOST_ENT structure in its memory area. (In the above, lp_hostinfo
holds the pointer value.) One of the structure members is h_addr_list
which is char * *. If I wish to walk the pointers and pick up all of
the addresses, how do I declare the smart pointer version of
lpp_addresses?

That depends on what lpp_adresses is actually pointing at, and who is
responsible for the memory that is being pointed at.

If you are not responsible for releasing any allocated memory, then a plain
pointer is just as safe as a smart pointer.

If you are responsible for the memory being pointed at, then an appropriate
smart pointer is indeed safer than a plain pointer.
What the appropriate smart pointer looks like, depends on the nature of the
things being pointed at.
From the description above, I gather that lpp_adresses points at an array
of C-style strings, thus the appropriate smart pointer is one that can
handle arrays.
If you have no such smart pointer on your hands, it might be an option to
copy the strings into a std::vector said:
Sincerely,

Gene Wirchenko

Bart v Ingen Schenau
 
M

Micah Cowan

CBFalconer said:
Please do not give C++ solutions in c.l.c.

Of course, this was also posted to alt.comp.lang.learn.c-c++,
where such an answer would be appropriate, were it not for the
blaring "[C]" in the subject line.
 
R

Richard Heathfield

Alan said:
Hi,

Try this:

Header File

stdlib.h

Category

Conversion Routines, Math Routines

Prototype

char *itoa(int value, char *string, int radix);

There is no such function in the C library.

The OP should be using sprintf.

Followups set to acllcc++
 
A

Alan Kelon

Hi,
I want to change a 3 digits integer to characters, how can i do that?
Try this:

Header File

stdlib.h

Category

Conversion Routines, Math Routines

Prototype

char *itoa(int value, char *string, int radix);

wchar_t *_itow(int value, wchar_t *string, int radix);

Description

Converts an integer to a string.

itoa converts value to a null-terminated string and stores the result in
string. With itoa, value is an integer. _itow is the unicode version of the
function. It converts an integer to a wide-character string.

radix specifies the base to be used in converting value; it must be between
2 and 36, inclusive. If value is negative and radix is 10, the first
character of string is the minus sign (-).

Note: The space allocated for string must be large enough to hold the
returned string, including the terminating null character (\0). itoa can
return up to 33 bytes.

Return Value

itoa returns a pointer to string.

Bye
 

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

Members online

Forum statistics

Threads
474,091
Messages
2,570,605
Members
47,225
Latest member
DarrinWhit

Latest Threads

Top