몽상실현개발주의

[따배씨] 6.16 배열 과 런타임 에러 본문

Language/C

[따배씨] 6.16 배열 과 런타임 에러

migrationArc 2021. 5. 20. 10:57

[따배씨] 6.16 배열 과 런타임 에러

따배씨 - 따라하며 배우는 C언어

6강 반복문

6.16 배열 Array 과 런타임 에러 Runtime error

#include <stdio.h>

#define NUM_DAYS 365

int main()
{
    char my_chars = "Hello, World!";
    
    int daily_temperature[NUM_DAYS];
    double stock_prices_history[NUM_DAYS];
    
    printf("%zd\n", sizeof(stock_prices_history));
    // 2920
    printf("%zd\n", sizeof(double) * NUM_DAYS);
    // 2920
    printf("%zd\n", sizeof(stock_prices_history[0]));
    // 8
    
    return 0;
}

 

#include <stdio.h>

int main()
{
    int my_numbers[5];
    
    my_numbers[0] = 1;  // subscripts, indices, offsets
    my_numbers[1] = 3;
    my_numbers[2] = 4;
    my_numbers[3] = 2;
    my_numbers[4] = 1024;
    
    printf("%d\n", my_numbers[0]);
    // 1
    printf("%d\n", my_numbers[1]);
    // 3
    printf("%d\n", my_numbers[2]);
    // 4
    printf("%d\n", my_numbers[3]);
    // 2
    printf("%d\n", my_numbers[4]);
    // 1024
    return 0;
}

 

#include <stdio.h>

int main()
{
    int my_numbers[5];
    
    my_numbers[0] = 1;  // subscripts, indices, offsets
    my_numbers[1] = 3;
    my_numbers[2] = 4;
    my_numbers[3] = 2;
    my_numbers[4] = 1024;
    
    /* Runtime Error */
    my_numbers[5] = 123;    // out of bound
    
    my_numbers = 10;    // Compile error
    
    printf("%d", my_numbers[10]);   // out of bound
    return 0;
}
  • Out of bound 의 경우 Runtime 단계에서 Error 발생
    • Compile time 과 Runtime 모두에서 Errorr 가 없는 프로그램을 작성하여야 한다

 

 


이 글의 모든 사진과 내용의 출처는 홍정모 교수님께 있음을 알려드립니다.

http://blog.naver.com/atelierjpro

 

실리콘 밸리의 프로그래머 : 네이버 블로그

안녕하세요! 홍정모 블로그에 오신 것을 환영합니다. 주로 프로그래밍 관련 메모 용도로 사용합니다. 강의 수강하시는 분들은 홍정모 연구소 카페로 오세요.

blog.naver.com

http://www.inflearn.com/course/following-c

 

홍정모의 따라하며 배우는 C언어 - 인프런 | 강의

'따배씨++'의 성원에 힘입어 새롭게 개발된 C 언어로 시작하는 프로그래밍 입문 강의입니다. '따배씨'와 함께 프로그래밍 인생을 업그레이드 해보세요., 따라하며 배우는 C언어 '따배씨++'의 성원

www.inflearn.com

Comments