C언어 질문있습니다.

2080fresh의 이미지

,<c>
#include <stdio.h>
#include <string.h>
 
int main()
{
	char str[100], res[100];
	int n, a=0, l, b;
 
	printf("Enter a sentence : ");
	fgets(str,100,stdin);
	printf("Enter a number,n : ");
	scanf("%d",&n);
 
	while(str[a]!=0){
		a++;
	}
	l=a-1;
 
	int r=0;
	n--;
	for (b=0;b<l;b++){
		if(n!=0){
			res[r]=str[b];
			if(str[b]==' '){
				n--;
			}
			r++;
		}
		else if(n==0){
			if(str[b]==' '){
				n=100;
			}
		}
	}
 
	printf("%s",res);
 
	return 0;
}
 
 
 
string문장을 입력받고, 또 입력받은 숫자n 번째의 단어를 삭제시켜 출력하는 코드입니다.
다른건 다 문제가 없는데, "a b c d e"로 string을 주고, n=5로 했을때 정상적이라면 "a b c d "가 출력되어야하는데, "a b c d ??"이런식으로 메모리 오류가 납니다. 예를들어 "ab cd ef gh ij"로 했을때는 n=5로 해도 잘 나오는데 말입니다. 어디에서 문제가 생긴걸까요?
정말 궁금합니다.
zasxer의 이미지

else if문 안에 res[r]=str[b]를 안넣어서 그렇습니다.
2080fresh의 이미지

else if문 안에 res[r]=str[b]를 넣으면 삭제가 안되지 않을까요?

 의 이미지

문자 배열 res이 null문자로 끝나지 않기 때문이지요.

주어진 코드를 잘 살펴보면 res 배열에 null문자를 넣는 경우가 없다는 것을 알 수 있습니다.

(zasxer님: else if문 안에 res[r]=str[b]를 넣더라도 마찬가지입니다. 프로그램 로직 상 그렇게 해도 되는지 안 되는지 따져보는 건 또 다른 문제이고요.)

아무튼, 그래서 정말로 신기한 건 "다른건 다 문제가 없다"는 부분입니다. 근데 원래 초기화되지 않은 지역변수에 어떤 값이 들어있는지는 모르는 일이라서요. 운 좋게 잘 될 수도 있는 거죠.

2080fresh의 이미지

그렇군요 제가 for문 끝나고 res[r]=NULL;을 붙였더니 되었어요!
그런데 컴파일을 할때 [Warning]assignment makes integer for pointer without a cast 라고 warning문구가 뜨는데 왜 그런건가요?

 의 이미지

NULL은 NULL 포인터를 초기화할 때 쓰는 매크로 상수입니다.
null문자는 그냥 0을 쓰거나 '\0'를 쓰면 됩니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.