전산 문제 중 C언어 버블정렬 &(엔퍼센드) 된것이 이해가 가지 않아 질문합니다

happyoht11의 이미지

시작하기 앞서 아래의 코드는 외부에서 퍼왔습니다.

안녕하세요. 제가 전산직 시험을 봤던 문제중 이해가 되지 않는 것이 있지 않아서 질문합니다.
아래 코드는 c로 만든 일반적인 버블정렬입니다.

#include<stdio.h>
 
void bubble_sort(int arr[], int n);
void main()
{
  int arr[]={35,3,12,9,1,20,15};
  int i;
  int n=sizeof(arr)/sizeof(*arr);
  bubble_sort(arr, n);
  printf("정렬결과 \n");
  for (i=0; i<n; i++)
  printf("%d \n", arr[i]);
 
}
 
void bubble_sort(int *p , int n)
 
{
 int i, j, tmp;
 
 for (i=0; i<n-1; i++)
 {
  for (j=0; j<n-i-1; j++)
  {
    if(*(p+j)>*(p+j+1))
    {
      tmp=*(p+j);
      *(p+j)=*(p+j+1);
      *(p+j+1)=tmp;
    }
  }
 }
}

위 코드에서 main 절에 보면
bubble_sort(arr, n); 이 있습니다.
여기서 제가 봤던 시험문제에서는 bubble_sort(&arr, &n); 으로 엔퍼센드 변수를 인수로 받더군요.
혹시 이렇게 된다면 bubble_sort 함수 내에서 어떻게 수정해야 정상적으로 출력이 되나요?
인터넷에 관련된 정보가 있나 찾아보는데 못찾겠네요ㅠㅠ 고수님들 부탁드립니다.

익명 사용자의 이미지

짠.
obscure하기로 유명한 C언어의 타입 문법을 조금 맛보시죠:

#include <stdio.h>
 
static void bubble_sort(int (*parr)[], int n);
 
int main() {
        int arr[] = {35, 3, 12, 9, 1, 20, 15};
        int n = sizeof(arr) / sizeof(*arr);
 
        bubble_sort(&arr, n);
 
        printf("정렬 결과:\n");
        for (int i=0; i < n; i++)
                printf("%d\n", arr[i]);
 
        return 0;
}
 
void bubble_sort(int (*parr)[], int n) {
        for (int i = 0; i < n - 1; i++)
        {
                for (int j = 0; j < n - i - 1; j++)
                {
                        if((*parr)[j] > (*parr)[j + 1])
                        {
                                int tmp = (*parr)[j];
                                (*parr)[j] = (*parr)[j + 1];
                                (*parr)[j + 1] = tmp;
                        }
                }
        }
}

사실 이런 형태의 코드를 구사하는 건 별로 실용적이지 않습니다.
typedef를 써서 명확히 하는 게 낫지요.

C언어 덕질(?)을 하는 소수의 너드들이나 이 따위 코드를 짜는데, 인터넷에 자료가 많을 리가요...

happyoht11의 이미지

와.... 금방 달아주셔서 감사합니다.
저도 살면서 저렇게 짜는건 처음봤네요. ㅠㅠ
그런데 저런 문제가 나온것도 신기합니다.....

Anti-Lock의 이미지

참고로...

호출하는 코드가 다음과 같이 바뀌더라도 함수내용은 변경하지 않아도 작동은 합니다.

bubble_sort(&arr, n);

bubble_sort 함수는 첫번재 인자로 int* 타입을 원하기 때문에 &arr[0] 로 적는것이 더 합당?하지만,
arr 를 넘겨주는 것도 가능합니다. 이게 C언어에서 무슨구칙때문인지 레퍼런스는 모르겠습니다.
익명분이 쓰신 글처럼 애초에 bubble_sort 함수의 인자 타입이 잘못되었다고 엄격하게 따질수도 있겠습니다.

어쨋거나.. bubble_sort 함수의 전방선언과 구현된 부분의 함수 인자들이 다른데, 같은내용으로 적는것이 좋습니다.

그리고 bubble_sort(&arr, &n); 처럼 하려면...

void bubble_sort(int *p , int* p_n)
{
int n = *p_n;
...
}

이렇게만 해도 되겠네요.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.