在visual studio中,我可以清楚地看到rstr()
(反向字符串)函数返回“olla \ 0”,但控制台显示屏显示不可读的字符符号。另外,由于某种原因,在调用printf()
之后,变量reverse
在调试模式下观察变量时也会转换为不可读的字符值。有谁知道如何正确显示rstr()
返回的字符串?
#include "stdio.h"
const char* rstr(const char* message) {
const int msg_size = sizeof(message) / sizeof(message[0]);
char r[msg_size + 1];
for (int i = 0; i < msg_size; ++i) {
r[i] = message[msg_size - i - 1];
if (i == (msg_size - 1))
r[msg_size] = '\0';
}
return r;
}
int main()
{
const char* reversed = rstr("allo");
printf("Reversed string is: %s\n", reversed);
getchar();
return 0;
}
答案 0 :(得分:2)
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("A1")) Is Nothing Then Exit Sub
On Error GoTo ReEnableEvents
Application.EnableEvents = False
Dim lastRow As Long
Dim r As Long
Dim c As Long
Dim newRow As Long
Dim Speciality As String
Speciality = Range("A1").Value
'clear existing data
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
If lastRow > 2 Then ' Assume headings are on row 2
Rows("3:" & lastRow).ClearContents
End If
'find new data
newRow = 2
With Worksheets("MasterList")
For r = 2 To .Cells(.Rows.Count, "A").End(xlUp).Row
For c = 2 To 11 ' loop through the speciality columns
If .Cells(r, c).Value = Speciality Then
newRow = newRow + 1
Cells(newRow, "A").Value = .Cells(r, "A").Value
Cells(newRow, "B").Value = .Cells(r, "L").Value
Cells(newRow, "C").Value = .Cells(r, "M").Value
Cells(newRow, "D").Value = .Cells(r, "N").Value
Cells(newRow, "E").Value = .Cells(r, "O").Value
Cells(newRow, "F").Value = .Cells(r, "P").Value
Cells(newRow, "G").Value = .Cells(r, "Q").Value
Exit For
End If
Next
Next
End With
ReEnableEvents:
Application.EnableEvents = True
End Sub
这里都错了。
消息是const int msg_size = sizeof(message) / sizeof(message[0]);
,因此char*
将是指向char的指针的大小。
由于sizeof(message)
是一个字符,message[0]
是一个定义。所以你的sizeof(message[0])
将是指向char的指针的大小。
您需要将String的长度传递给msg_size
函数。