Dear group,
if I want to allocate a string with a help of malloc then, shall
I use the following in a loop?
char *s;
s = malloc(strlen(s)+1);
Any pointer reference is help full. I went through the K & R
book's chapter 5. But I am not able to get the answer. 9 2108
"fool" <fo**@fool.comwrote in message
>
if I want to allocate a string with a help of malloc then, shall
I use the following in a loop?
char *s;
s = malloc(strlen(s)+1);
Any pointer reference is help full. I went through the K & R
book's chapter 5. But I am not able to get the answer.
At a guess, you are getting a string character by character, maybe from a
file.
What you want is this
char *s = 0;
char ch;
In N = 0;
do
{
ch = getacharacter();
N++;
s = realloc(s, N);
if(!s)
{
printf("Out of memory\n");
exit(EXIT_FAILURE);
}
s[N-1] = ch;
} while(ch != 0);
Modify the code if the string is termianted by newline (you will have to
replace the last charcter by a 0).
-- www.personal.leeds.ac.uk/~bgy1mm
freeware games to download.
Malcolm said:
<snip>
What you want is this
<snip>
s = realloc(s, N);
Never wise.
<snip>
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at the above domain, - www.
In article <uJ******************************@bt.com>, re*******@btinternet.com says...
"fool" <fo**@fool.comwrote in message
if I want to allocate a string with a help of malloc then, shall
I use the following in a loop?
char *s;
s = malloc(strlen(s)+1);
Any pointer reference is help full. I went through the K & R
book's chapter 5. But I am not able to get the answer.
At a guess, you are getting a string character by character, maybe from a
file.
What you want is this
char *s = 0;
char ch;
In N = 0;
do
{
ch = getacharacter();
N++;
s = realloc(s, N);
if(!s)
{
printf("Out of memory\n");
exit(EXIT_FAILURE);
}
s[N-1] = ch;
} while(ch != 0);
Modify the code if the string is termianted by newline (you will have to
replace the last charcter by a 0).
Am I correct with the following:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
char *s=0,ch, length;
int n,i;
do
{
fgets(s, sizeof s, stdin);
n++;
s = realloc(s, n);
if(!s)
exit(EXIT_FAILURE);
ch = *s++;
s[n-1] = ch;
length = strlen(s);
}while(ch != 0 );
for(i=0 ; i < length ; i++)
printf("%c\n",s[i]);
return 0;
}
But gcc gives me a RT error.
fool said:
<snip>
>
Am I correct with the following:
No.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
char *s=0,ch, length;
int n,i;
do
{
fgets(s, sizeof s, stdin);
fgets requires a pointer to the first byte of memory it can use. But s is a
null pointer, so it doesn't point to any memory fgets can use. And the
second argument is supposed to say how many bytes of memory are available
at that address, to which the best answer you could reasonably give is: 0.
If you want to put stuff in a box, first get a box.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at the above domain, - www.
In article <2r*********************@bt.com>, rj*@see.sig.invalid says...
fool said:
<snip>
Am I correct with the following:
No.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
char *s=0,ch, length;
int n,i;
do
{
fgets(s, sizeof s, stdin);
fgets requires a pointer to the first byte of memory it can use. But s is a
null pointer, so it doesn't point to any memory fgets can use. And the
second argument is supposed to say how many bytes of memory are available
at that address, to which the best answer you could reasonably give is: 0.
If you want to put stuff in a box, first get a box.
Me and my foolish brain!!
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
char *s=0,ch;
int n=0;
do
{
s = realloc(s, n);
if(!s)
exit(EXIT_FAILURE);
fgets(s, sizeof s, stdin);
n++;
ch = *s;
s[n-1] = ch;
printf("%s",s);
}while(ch != 0 && (*s) != '\0'); /* derefer s and till s
reaches '\0', correct?*/
return 0;
}
fool said:
<snip>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
char *s=0,ch;
int n=0;
do
{
s = realloc(s, n);
if(!s)
exit(EXIT_FAILURE);
This is not a good idea, but it'll do for now, except for the fact that
you're allocating ZERO bytes of memory on the first time round.
fgets(s, sizeof s, stdin);
sizeof s is wrong - s has type char *, so sizeof s is equivalent to
sizeof(char *), which isn't going to change any time soon. What you should
put here instead is the number of bytes available at the memory pointed to
by s - in this case, that would be n.
What if fgets failed? (It returns NULL if it encounters an error or an
end-of-file condition.) You need to detect this.
n++;
ch = *s;
copies first character of the input string to ch
s[n-1] = ch;
copies ch to the (n-1)th character of s - is that *really* what you want to
do? Especially if it overwrites your terminating null character (which
seems possible to me, although I admit I haven't read the code very
closely).
printf("%s",s);
}while(ch != 0 && (*s) != '\0'); /* derefer s and till s
reaches '\0', correct?*/
I think you need to take a step back and decide exactly what it is that
you're trying to do. Do you just want to read one string from stdin, or
many? Do you want to store one string, or many? What are you aiming for? If
you can answer this, perhaps we can help you better.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at the above domain, - www.
fool wrote:
rj*@see.sig.invalid says...
>fool said:
<snip>
>>> Am I correct with the following:
No.
>>#include<stdio.h> #include<stdlib.h> #include<string.h> int main(void) { char *s=0,ch, length; int n,i; do { fgets(s, sizeof s, stdin);
fgets requires a pointer to the first byte of memory it can use. But s is a null pointer, so it doesn't point to any memory fgets can use. And the second argument is supposed to say how many bytes of memory are available at that address, to which the best answer you could reasonably give is: 0.
If you want to put stuff in a box, first get a box.
Me and my foolish brain!!
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
char *s=0,ch;
int n=0;
do
{
s = realloc(s, n);
No. The very first time you are allocating 0 bytes. This can't
hold anything. In addition reallocating for each input character
is an expensive operation.
if(!s)
exit(EXIT_FAILURE);
fgets(s, sizeof s, stdin);
n++;
ch = *s;
s[n-1] = ch;
printf("%s",s);
}while(ch != 0 && (*s) != '\0'); /* derefer s and till s
reaches '\0', correct?*/
return 0;
}
Now that you have tried various things out, download ggets and see
how I handle it. You can find the complete module at:
<http://cbfalconer.home.att.net/download/>
and it will probably be quite understandable after your efforts.
Most of the download is testing code and demonstration usage. It
then adds up to a whopping 11k byte download. the reallocation
strategy is optimized for the most usual usage of interactive
input, when very long input strings are extremely rare, and usually
require keyboard walking cats.
rjh has an alternative method. See readme.txt file in ggets.zip.
--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
fool wrote:
In article <2r*********************@bt.com>, rj*@see.sig.invalid says...
>fool said:
<snip>
>>Am I correct with the following:
No.
>>#include<stdio.h> #include<stdlib.h> #include<string.h> int main(void) { char *s=0,ch, length; int n,i; do { fgets(s, sizeof s, stdin);
fgets requires a pointer to the first byte of memory it can use. But s is a null pointer, so it doesn't point to any memory fgets can use. And the second argument is supposed to say how many bytes of memory are available at that address, to which the best answer you could reasonably give is: 0.
If you want to put stuff in a box, first get a box.
Me and my foolish brain!!
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
char *s=0,ch;
int n=0;
do
{
s = realloc(s, n);
if(!s)
exit(EXIT_FAILURE);
fgets(s, sizeof s, stdin);
n++;
ch = *s;
s[n-1] = ch;
printf("%s",s);
}while(ch != 0 && (*s) != '\0'); /* derefer s and till s
reaches '\0', correct?*/
return 0;
}
Several things I don't understand about your code but here's mine.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char line[80];
char *t, *s = NULL;
int n, len = 0;
while (fgets(line, sizeof line, stdin)) {
n = strlen(line);
t = realloc(s, len + n+1);
if (!t) puts("alloc failure."), exit(EXIT_FAILURE);
s = t;
strcpy(s+len, line);
len = strlen(s);
}
printf("%s", s);
return 0;
}
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
On Sat, 23 Dec 2006 12:27:48 +0530, fool <fo**@fool.comwrote in
comp.lang.c:
There is a serious error in your concept below.
Dear group,
if I want to allocate a string with a help of malloc then, shall
I use the following in a loop?
char *s;
Assuming that the definition of s, above, is inside a function, you
have created an uninitialized pointer, that is it does not contain the
address of one or more characters that belong to your program.
s = malloc(strlen(s)+1);
Passing the uninitialized pointer value 's' so strlen(), as you will
do on the first pass through the loop, produces undefined behavior and
quite possibly will cause your operating system to shut down your
program.
Any pointer reference is help full. I went through the K & R
book's chapter 5. But I am not able to get the answer.
I am not completely sure what it is you are trying to do. You have
not shown us enough of your code. The correction is different if you
are trying to allocate memory to copy an existing string, or if you
are trying to allocate memory for a string coming from an input
stream.
You should post again, and include the complete function that you are
trying to write, with an explanation of what it's inputs are and
exactly what you want the result to be.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Knady |
last post by:
Hi,
I have the following problem, I must to do my assignment, but I really do
not know how to use the malloc.
I need create a program that will be...
|
by: sathyashrayan |
last post by:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct tree
{
int data;
struct tree *left,*right;
};
void init(struct tree *node)
|
by: rory |
last post by:
Cna anyone point me in the right direction, I have a struture in my .h
file:
typedef struct mdata
{
int names;
int dates;
int ages;
}m_Data;
|
by: Aris |
last post by:
A few days ago I asked from you how to join a string like "file"
A number that change values from 1 to 5 and another string like ".txt"
to have...
|
by: Marcel |
last post by:
I am C++ beginner.
When i start building very basic things like hello world programs i am very
attired by the <string> library so i put all text...
|
by: Yourko |
last post by:
Hi there! I`me currently trying to write some simple programs in C.
For one such program i created globals.h file. In that file i defined a...
|
by: raam |
last post by:
hi all,
I trying to write an array of integers into a text
file.While writing into the file it works.I used fwrite function, but
when I read the...
|
by: nodrogbrown |
last post by:
hi
i want to create an array of strings .I know the number of items that
the array will contain but the size of each string will be different
and...
|
by: lgerhardx |
last post by:
First of all, please forgive my newness to the language. I have had
much experience programming...but not in C! :)
I am modifying a program...
|
by: better678 |
last post by:
Question:
Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct?
Answer:
Java is an object-oriented...
|
by: CD Tom |
last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
|
by: CD Tom |
last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: jalbright99669 |
last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
| |