C的指標名稱及陣列名稱關係

這是一個bubble sort的小程式⋯陣列名稱=陣列的起始位置,所以我一開始是覺得用指標宣告也行⋯但沒想到⋯不太懂為什麼不能使用指標!

img

img

You declare a char pointer called str, but memory is not allocated to it. When you try to scanf, it will surely encounter segment fault error. Array is a data struct that memory is pre-allocated, so it works.

On the other hand, choosing char is not very wise, cause it take 1 byte with very a limited number range.

仅声明char *,没有内存用来保存输入的字符串.