Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Signal 11 error

Posted on 2008-06-20
5
Medium Priority
?
2,068 Views
Last Modified: 2008-06-23
I am getting a signal 11 error for strcpy function in Linux.
#include <stdio.h>
 
#define KEY_LEN                30
#define NO         0
 
int main()
{
char num[KEY_LEN+1];
 
printf("\nbefore strcpy");
strcpy(num , NO );
printf("\nafter strcpy %d",NO);
printf("\nafter strcpy %s",num);
}

Open in new window

0
Comment
Question by:bokaria
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 21830679
You cannot copy an integer value into a string.

Did you mean to use :

        sprintf(num, "%d", NO);
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 21830682
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 150 total points
ID: 21830690
>strcpy(num , NO );
Is effectively
strcpy(num , NULL );

You are trying to read memory location 0 which in not readable ... hence you get a SIGSEGV or signal 11 or segmentation fault.
http://linux.die.net/man/7/signal

To copy number 0, use

#include <stdio.h>
 
#define KEY_LEN                30
#define NO         "0" 
//Not the quotes "0"
 
int main()
{
char num[KEY_LEN+1];
 
printf("\nbefore strcpy");
strcpy(num , NO );
printf("\nafter strcpy %s",NO); // we use %s to print the string "0"
printf("\nafter strcpy %s",num);
}

Open in new window

0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 21830710
>To copy number 0, use
Is probably not how intended it to sound ... I meant
To copy string "0" ... i.e. string consisting of a single char - 0
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 21852764
Didn't you want to use an integer value, bokaria ? If so, take a look at what I posted.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Fine Tune your automatic Updates for Ubuntu / Debian
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question