LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-02-2015, 05:36 AM   #1
savio
LQ Newbie
 
Registered: May 2015
Posts: 29

Rep: Reputation: Disabled
"segmentation fault: 11" with sprintf in C


Hi,

I have problems with the strings size.
My program is the following:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {

  FILE * fp;
  char input[20];
  char *str;

  printf("Enter the file name: ");
  scanf("%s", input);
  printf("The file name is %s\n", input);
  printf("Sizeof %s is %lu\n", input, sizeof(input));

  fp = fopen("file.txt", "w");
  fprintf(fp, "%s", input);
  fclose(fp);

  sprintf(str, "%s", input);
  puts(str);

  return 0;

}
Why fprintf works well whereas the sprintf causes the segmentation fault?
I noticed that changing the size of the input array from 20 to (at least) 41 solves the problem and sprintf works as well. Why?
 
Old 07-02-2015, 06:12 AM   #2
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

Quote:
Why fprintf works well whereas the sprintf causes the segmentation fault?
The problem is the str pointer. You did not initialized it to point to a valid memory area, hence segfault when sprintf() tries to write to this (invalid) memory area.

You should allocate a big enough buffer before trying to write to it:
Code:
char *str = malloc(sizeof(char)*20);
or
Code:
char str[20]; // static allocation
Quote:
I noticed that changing the size of the input array from 20 to (at least) 41 solves the problem and sprintf works as well. Why?
Probably a compiler/operating system quirk. On my machine increasing length of input does not prevents segfault (fortunately).

Few links: C pointers and arrays, C memory management.

Last edited by firstfire; 07-02-2015 at 06:25 AM. Reason: Add links.
 
1 members found this post helpful.
Old 07-02-2015, 08:08 AM   #3
savio
LQ Newbie
 
Registered: May 2015
Posts: 29

Original Poster
Rep: Reputation: Disabled
It is true.
Thanks a lot!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] The expect -c "spawn ls" check in section 6.12 of the book gives a segmentation fault ezekielrage Linux From Scratch 1 05-18-2011 08:58 AM
"netstat -l" gives "segmentation fault" Earwig Linux - Newbie 2 03-31-2010 04:12 PM
sunbird 0.3 does not start, error: Segmentation fault : "$prog" ${1+"$@"} polemon Ubuntu 8 01-08-2007 04:22 AM
programming-segmentation fault(sprintf) ramakrishna sura Linux - General 1 12-20-2005 04:12 AM
why iam getting problems like "...too many files opened" or "segmentation fault" naren_0101bits Linux - Newbie 2 07-19-2004 12:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration