Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2079
  • Last Modified:

Signal 11 error

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
bokaria
Asked:
bokaria
  • 3
  • 2
1 Solution
 
Infinity08Commented:
You cannot copy an integer value into a string.

Did you mean to use :

        sprintf(num, "%d", NO);
0
 
Infinity08Commented:
0
 
sunnycoderCommented:
>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
 
sunnycoderCommented:
>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
 
Infinity08Commented:
Didn't you want to use an integer value, bokaria ? If so, take a look at what I posted.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now