String Manipulation error

S

selvesteen

Hello All,

I am getting following error on HP-UX for the program given below

"Pid 10150 in trap loop, signal 11
Memory fault"

int main()
{
char *txtptr="";
const char *a = "string append";
strcat(txtptr,a);
printf("%s\n",txtptr);
}
What is wrong with this program? What is a better method to initialize
a string (char *txtptr="") with NULL.

Advance thanks for the help.
Mike
 
D

David Resnick

Hello All,

I am getting following error on HP-UX for the program given below

"Pid 10150 in trap loop, signal 11
Memory fault"

int main()
{
char *txtptr="";
const char *a = "string append";
strcat(txtptr,a);
printf("%s\n",txtptr);
}
What is wrong with this program? What is a better method to initialize
a string (char *txtptr="") with NULL.

Advance thanks for the help.
Mike

Try the FAQ:

http://www.eskimo.com/~scs/C-faq/q7.2.html

You need to read up about memory allocation...

-David
 
Y

Yohji

Hello All,

I am getting following error on HP-UX for the program given below

"Pid 10150 in trap loop, signal 11
Memory fault"

int main()
{
char *txtptr="";
const char *a = "string append";
strcat(txtptr,a);
printf("%s\n",txtptr);
}
What is wrong with this program? What is a better method to initialize
a string (char *txtptr="") with NULL.

Advance thanks for the help.
Mike

Yeah,this is one of the common mistakes.That is just because you did
not alloc enough mem for 'txtptr'.You should know that the function
strcat() does NOT alloc mem for you.So you can try this:

#define MAX 255
....
char txtptr[MAX]="";
 
E

Eric Sosman

Hello All,

I am getting following error on HP-UX for the program given below

"Pid 10150 in trap loop, signal 11
Memory fault"

int main()
{
char *txtptr="";
const char *a = "string append";
strcat(txtptr,a);
printf("%s\n",txtptr);
}
What is wrong with this program? What is a better method to initialize
a string (char *txtptr="") with NULL.

It may not look like it at first glance, but this is
Question 7.1 in the comp.lang.c Frequently Asked Questions
(FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html
 
A

Alexei A. Frounze

Hello All,

I am getting following error on HP-UX for the program given below

"Pid 10150 in trap loop, signal 11
Memory fault"

int main()
{
char *txtptr="";
const char *a = "string append";
strcat(txtptr,a);
printf("%s\n",txtptr);
}
What is wrong with this program? What is a better method to initialize
a string (char *txtptr="") with NULL.

Advance thanks for the help.
Mike

Did you learn basic or pascal before? That would explain it :)

Alex
 
M

Martin Ambuhl

int main()
{
char *txtptr="";
const char *a = "string append";
strcat(txtptr,a);
printf("%s\n",txtptr);
}
What is wrong with this program?

The usual things, asked about every day by people who neither read the
FAQ nor follow the newsgroups.
1) The big one: txtptr is points to a string literal which is one char
wide; that string literal neither is guaranteed to be writable nor has
any additional space assigned to it.
failure to #include said:
What is a better method to initialize
a string (char *txtptr="") with NULL.

You don't want to initialize the string with NULL, which is the name of
a null-pointer constant.

#define BIGENOUGH 4096 /* or whatever is big enough */
[...]
char txt[BIGENOUGH] = "";
 

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

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top