473,960 Members | 19,480 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with char array

Hi,

I'm reading the book "The C programming Language". I'm trying to do an
exercise asking to find the longest line. I'm reading the book in
sequence hence I shouldn't know anything about pointer yet.

As a string is declared with "char string[n]", the length is already
specified, how can I store an arbitrary long string? I've tried to do
something like

char string[10];
....
char string2[20];
string = string2;

but it complains about incompatible types. And it wouldn't work either
if I use a 2 dimensional character array as the total length is still
fixed.

Thanks for help.
Pashmina
Nov 14 '05 #1
11 1642
pa*******@inter free.it (pashmina g.) writes:
I'm reading the book "The C programming Language". I'm trying to do an
exercise asking to find the longest line. I'm reading the book in
sequence hence I shouldn't know anything about pointer yet.

As a string is declared with "char string[n]", the length is already
specified, how can I store an arbitrary long string?


I don't think you can without pointers. However, you don't need to store
a line, or even part of a line, at any time, if you only want to find
out which line is the longest.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #2
nrk
Martin Dickopp wrote:
pa*******@inter free.it (pashmina g.) writes:
I'm reading the book "The C programming Language". I'm trying to do an
exercise asking to find the longest line. I'm reading the book in
sequence hence I shouldn't know anything about pointer yet.

As a string is declared with "char string[n]", the length is already
specified, how can I store an arbitrary long string?
I don't think you can without pointers. However, you don't need to store
a line, or even part of a line, at any time, if you only want to find
out which line is the longest.


The trick can be extended, so that you go through the file twice and print
the longest line without having to store any part of it.

-nrk.
Martin


--
Remove devnull for email
Nov 14 '05 #3
The exercise also asks to print out the text of the longest line. Those
lines are read from standard input, I shouldn't know anything about
reading nor writing a file yet. In fact I've no idea of how to do that
assuming that I should use only those things taught in previous chapter
(variables and arithmetic expressions, for, symbolic constant, character
input/output, array, function and character arrays).

Pashmina

On Thu, 26 Feb 2004 13:14:45 +0100
Martin Dickopp <ex************ ****@zero-based.org> wrote:
pa*******@inter free.it (pashmina g.) writes:
I'm reading the book "The C programming Language". I'm trying to do
an exercise asking to find the longest line. I'm reading the book in
sequence hence I shouldn't know anything about pointer yet.

As a string is declared with "char string[n]", the length is already
specified, how can I store an arbitrary long string?


I don't think you can without pointers. However, you don't need to
store a line, or even part of a line, at any time, if you only want to
find out which line is the longest.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-.
=.
/ ,- ) http://www.zero-based.org/ ((_/)o
o(\_))\ `-'
`-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/

--
Nov 14 '05 #4
"pashmina g." <pa*******@inte rfree.it> writes:
On Thu, 26 Feb 2004 13:14:45 +0100
Martin Dickopp <ex************ ****@zero-based.org> wrote:
pa*******@inter free.it (pashmina g.) writes:
> I'm reading the book "The C programming Language". I'm trying to do
> an exercise asking to find the longest line. I'm reading the book in
> sequence hence I shouldn't know anything about pointer yet.
>
> As a string is declared with "char string[n]", the length is already
> specified, how can I store an arbitrary long string?


I don't think you can without pointers. However, you don't need to
store a line, or even part of a line, at any time, if you only want to
find out which line is the longest.


The exercise also asks to print out the text of the longest line. Those
lines are read from standard input, I shouldn't know anything about
reading nor writing a file yet. In fact I've no idea of how to do that
assuming that I should use only those things taught in previous chapter
(variables and arithmetic expressions, for, symbolic constant, character
input/output, array, function and character arrays).


Please don't top-post (fixed), and please don't quote irrelevant parts
like signatures.

If you cannot use pointers (which means you cannot use dynamic memory
allocation), and if you cannot assume a fixed upper bound on the line
length, there's no solution which works for any input.

I suggest that you write a program which works if no line is longer
than, say, 255 characters. However, the program should /not/ assume that
that is true for the input provided to it. If it encounters a longer
line, it should print a meaningful error message. Alternatively, you
could write it so that it only prints the first 255 characters of the
longest line if that line is longer than that.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #5
pashmina g. wrote:
The exercise also asks to print out the text of the longest line.


If this is Exercise 1-16, note the words "and as much as possible of the
text". This gives you the freedom to store only up to MAXLEN (define this
as you wish) bytes of the line.

Suffice to say that it /is/ possible to store arbitrarily long strings in C,
but to do that (at least, in a portable and robust way) requires knowledge
which you have not yet acquired in Chapter 1 of K&R2.

<snip>

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #6
> The trick can be extended, so that you go through the file twice and print
the longest line without having to store any part of it.


You don't have to go through the file twice.

You can have a byte index for the start of the line and the line length,
both stored in two size_t variables.

After going through the file once, you'll have a valid index and you can
use fseek() and fread() to read the line content. It's not quite like
"going through the file twice". ;-)

Nov 14 '05 #7
Guillaume wrote:
The trick can be extended, so that you go through the file twice
and print the longest line without having to store any part of it.


You don't have to go through the file twice.

You can have a byte index for the start of the line and the line
length, both stored in two size_t variables.

After going through the file once, you'll have a valid index and you
can use fseek() and fread() to read the line content. It's not quite
like "going through the file twice". ;-)


Depending on the file and system, you cannot always seek etc. The
only safe method is to use something like ggets (available on my
page) and have two buffered lines available - the current one and
the longest so far. Now the only assumption needed is that you
can read the file once up to EOF.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #8
> Depending on the file and system, you cannot always seek etc. The
only safe method is to use something like ggets (available on my
page) and have two buffered lines available - the current one and
the longest so far. Now the only assumption needed is that you
can read the file once up to EOF.


True, and with the standard input, fseek() is not an option, nor is
going backwards...

Nov 14 '05 #9
Hi,
char longestline [1000] ; /* The Longest Line i expect */

DONT allocate memory at run time until it is obsolutely neccessary.

If u know what a buffer size must be maximum. Fix it and use it.

Regards,
Ram
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1430
by: Krzysztof | last post by:
Hi! I have a class: some_class{ private: char* seq; public: some_class(char* se); char* ret_seq(); ....
28
2148
by: Davy | last post by:
Hi all, I found char x={"my"}; can be compiled. But char x; x={"my"}; can not be compiled.
6
2947
by: Edd Dawson | last post by:
Hi. I have a strange problem involving the passing of command line arguments to a C program I'm writing. I tried posting this in comp.programming yesterday but someone kindly suggested that I'd have better luck here. So here goes! My program ignores any command line arguments, or at least it's supposed to. However, when I pass any command line arguments to the program, the behaviour of one of the functions changes mysteriously. I have...
3
2492
by: ritchie | last post by:
Hi all! Still working on this program! Just to recap, I am writing a program to sort an array with four different sort algorythms. I am having a little trouble at the moment though! Now, I am trying to calculate, with each sort, how many times during the sort the array elements are compared and swapped.
8
10743
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to receive a byte array as one of its parameters. The project is marked for COM interop, and that all proceeds normally. When I reference the type library in the VB6 project, and write the code to call the function that returns the byte array, it works
2
4473
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c */ #include <time.h>
39
19738
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1) What's the difference between these 3 statements: (i) memcpy(&b, &KoefD, n); // this works somewhere in my code
12
5846
by: NOO Recursion | last post by:
Hi everyone! I am trying to write a program that will search a 12x12 for a thing called a "blob". A blob in the grid is made up of asterisks. A blob contains at least one asterisk. If an asterisk is in a blob, an asterisk that is contiguous to it is in the same blob. If a blob has more than two asterisks, then each asterisk in the blob is contiguous to at least one other asterisk in the blob. For example this 12x12 grid has 6 blobs. ...
5
2572
by: weidongtom | last post by:
Hi, I tried to implement the Universal Machine as described in http://www.boundvariable.org/task.shtml, and I managed to get one implemented (After looking at what other's have done.) But when I use to run a UM program, I kept on getting error messages. I have used someone else's implementation and it runs fine. I have compared my code with other's and I still can't figure it out what's wrong with mine. So please help me out, after 3...
9
2528
by: weidongtom | last post by:
Hi, I've written the code that follows, and I use the function add_word(), it seems to work fine *before* increase_arrays() is called that uses realloc() to allocate more memory to words. But *after* calling increase_arrays(), I received segmentation fault. I tried to step it through gdb, and I found out that after calling increase_arrays(), words's original value is modified, and if I tried to access it, I get <address 0x11 out of...
0
10273
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11731
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10816
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8391
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7548
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6320
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6460
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
5072
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4659
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.