Hi!
i'm a newbie in C language and i'm writing my first simple codes.
In one of these, my purpose is to append the ascii value of an interger
(example 101 --> e) at the end of a string to obtain a new (longer)
string. Example:
string: languag
letter: e
string (new value): language
In compiling, i received "Segmentation fault" error but i
can't understand why.
This is the very simple code:
main()
{
char* output="languag";
int i = 101;
char* letter;
letter[0]=(char)i;
letter[1]='\0';
printf("%s\n",letter); //prints "e"
//next line is not executed
strcat(output,letter); //segmentation fault printf("%s\n",output);
}
Thank you :) 11 4696
On Mon, 09 Aug 2004 11:44:08 +0000, Polar wrote: main() { char* output="languag"; int i = 101; char* letter; letter[0]=(char)i; letter[1]='\0'; printf("%s\n",letter); //prints "e" //next line is not executed strcat(output,letter); //segmentation fault printf("%s\n",output);
}
Sorry fo incorrect line-wrap.
This is the code:
main()
{
char* output="languag";
int i = 101;
char* letter;
letter[0]=(char)i;
letter[1]='\0';
printf("%s\n",letter); //prints "e"
//next line is not executed
strcat(output,letter); //segmentation fault
printf("%s\n",output);
}
Thank you :)
On 2004-08-09, Polar <po***@pole.po> wrote: Hi!
In compiling, i received "Segmentation fault" error but i can't understand why.
Probably not "in compiling" but "in running", but anyway.
main() { char* output="languag"; int i = 101; char* letter;
^^^^^^^
This is a pointer to a character. Currently it points to a random
address inside the computer's "memory". It points to an address that
possibly (and probably) does not "belong" to your "program". Or it can
point to an address where other usefull information is stored, like
the code of your program, or other variables of your program, or
anything.
letter[0]=(char)i; letter[1]='\0';
Now you try to write something to this random address (that probably
doesn't even belong to you), as well as to the address next to it. A
segfault is imminent!
/npat
Polar wrote on 09/08/04 : main() { char* output="languag";
string literals are not guaranteed to be writable. Better to define
them read-only:
char const * output="languag";
int i = 101;
What is this magic value supposed to represent? If you want 'e', just
use 'e'.
char* letter;
Warning. This variable is not initialized, meaning that its value is
undetermined. Being a pointer, it points anywhere.
letter[0]=(char)i;
Dereferencing an undefined pointer invokes an undefined behaviour (UB).
Your program is dead. Fix it.
letter[1]='\0';
UB again.
printf("%s\n",letter); //prints "e"
UB again. (Passing an undefined value to a function)
//next line is not executed strcat(output,letter); //segmentation fault
UB again for many reasons:
- 'letter' is still undefined
- 'output' is not writable
- Should it be writable, there is no room enough for "e"
printf("%s\n",output); }
You must use array oc ahrs to do what you want. Memory space doesn't
come by magic.
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
"C is a sharp tool"
"Polar" <po***@pole.po> wrote in message
news:pa****************************@pole.po... On Mon, 09 Aug 2004 11:44:08 +0000, Polar wrote:
main() { char* output="languag"; int i = 101; char* letter; letter[0]=(char)i; letter[1]='\0'; printf("%s\n",letter); //prints "e" //next line is not executed strcat(output,letter); //segmentation fault printf("%s\n",output);
} Sorry fo incorrect line-wrap.
This is the code:
main() { char* output="languag";
'output' is readonly !!
int i = 101; char* letter; letter[0]=(char)i;
/* No need of a cast here*/
letter[1]='\0'; printf("%s\n",letter); //prints "e" //next line is not executed strcat(output,letter); //segmentation fault
Sure it is segmentation fault..Change the declaration to
char output[32]="languag"; /* Note 32 is a magic number (avoid it
whereever possible */
Ditto for char *letter; change to
char letter[32];
printf("%s\n",output);
}
Thank you :)
Ravi Uday wrote on 09/08/04 : int i = 101; char* letter; letter[0]=(char)i;
/* No need of a cast here*/
Yes, but you missed the point. 'letter' is not initialized. The array
is no-existing. 'letter[0]' invokes a UB.
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
"C is a sharp tool"
Emmanuel Delahaye wrote: The array is no-existing.
I'd write inexistent (close to French spelling) or nonexistent ;-)
Grumble wrote on 09/08/04 : Emmanuel Delahaye wrote:
The array is no-existing.
I'd write inexistent (close to French spelling) or nonexistent ;-)
Yes, sorry. Thanks for correcting me. I know it's boring to read bad
English.
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
"C is a sharp tool"
Polar <po***@pole.po> spoke thus: main()
main() returns an int. The return type will not default to int under
C99, and it's a good idea to specify its return value even under C89.
Prefer the following:
int main( void )
Other people have already pointed out the actual error(s) in your
code.
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
(supersedes <mn***********************@YOURBRAnoos.fr>)
Polar wrote on 09/08/04 : main() { char* output="languag";
string literals are not guaranteed to be writable. Better to define
them read-only:
char const * output="languag";
int i = 101;
What is this magic value supposed to represent? If you want 'e', just
use 'e'.
char* letter;
Warning. This variable is not initialized, meaning that its value is
undetermined. Being a pointer, it points anywhere.
letter[0]=(char)i;
Dereferencing an undefined pointer invokes an undefined behaviour (UB).
Your program is dead. Fix it.
letter[1]='\0';
UB again.
printf("%s\n",letter); //prints "e"
UB again. (Passing an undefined value to a function)
//next line is not executed strcat(output,letter); //segmentation fault
UB again for many reasons:
- 'letter' is still undefined
- 'output' is not writable
- Should it be writable, there is no room enough for "e"
printf("%s\n",output); }
You must use array of chars to do what you want. Memory space doesn't
come by magic.
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
"C is a sharp tool"
[cut]
Thanks to all my teachers :)
Now I understand my errors.
[cut]
Thanks a lot to everyone :) Now I understood my errors :) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: fudmore |
last post by:
Hello Everybody.
I have a Segmentation fault problem. The code section at the bottom
keeps throwing a Segmentation fault when it enters the IF block for the
second time.
const int...
|
by: Matthew Jakeman |
last post by:
can anyone tell me why the following piece of code causes a segmentation fault please ??
char *getFNFromIden(char *ident)
{
int i = 0 ;
printf("identifier %s\n", allMenus.identifier) ;...
|
by: Zheng Da |
last post by:
Program received signal SIGSEGV, Segmentation fault.
0x40093343 in _int_malloc () from /lib/tls/libc.so.6
(gdb) bt
#0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6
#1 0x40094c54 in malloc...
|
by: Fra-it |
last post by:
Hi everybody,
I'm trying to make the following code running properly, but I can't get
rid of the "SEGMENTATION FAULT" error message when executing.
Reading some messages posted earlier, I...
|
by: Paminu |
last post by:
I have a wierd problem.
In my main function I print "test" as the first thing. But if I run the call
to node_alloc AFTER the printf call I get a segmentation fault and test is
not printed!
...
|
by: pycraze |
last post by:
I would like to ask a question. How do one handle the exception due to
Segmentation fault due to Python ? Our bit operations and arithmetic
manipulations are written in C and to some of our...
|
by: madunix |
last post by:
My Server is suffering bad lag (High Utlization)
I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2
Web visitors retrieve data from the web by php calls through oci
cobnnection...
|
by: DanielJohnson |
last post by:
int main()
{
printf("\n Hello World");
main;
return 0;
}
This program terminate just after one loop while the second program
goes on infinitely untill segmentation fault (core dumped) on...
|
by: jr.freester |
last post by:
I have created to classes Matrix and System. System is made up of
type matrix.
----------------------------------------------------------------------------------
class Matrix
{
private:
int...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |