#include <stdio.h>
#include <stdlib.h>
struct test
{
int id;
char name[20];
};
int main()
{
struct test t1;
t1.id=1;
fflush(stdin);
fgets(t1.name,20,stdin);
print((&t1.name));
print1(t1.id,&(t1.name));
}
void print(struct test *name)
{
puts(name);
}
void print1(struct test id,struct test *name)
{
printf("\n%d\n",id);
puts(name);
}
当我运行此程序时,它会要求输入
测试[输入]
输出出来
测试 1 (然后程序终止)
为什么第一次投入工作以及为什么第二次投入不起作用?是的,有一个发送完整结构的选项,但我想知道这里有什么问题。
答案 0 :(得分:2)
由于以下几个原因,您的计划无效:
void print1(int id, char *name)
或者你应该通过值或指针传递整个结构,即void print1(struct test t)
一旦解决了这两个问题,并确保程序编译无警告,并启用所有编译器警告,问题就应该解决了。
答案 1 :(得分:1)
void print(struct test *name)
应改为
void print(char name[]) // because you wish to print a null terminated array of characters.
print((&t1.name));
应改为
print(t1.name); //name is the array you wish to print
答案 2 :(得分:0)
你需要这个
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.login_fragment_layout, container, false);
final EditText password = (EditText) view.findViewById(R.id.password);
// Workaround https://issuetracker.google.com/issues/37082815 for Android 4.4+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && isRTL(getActivity())) {
// Force a right-aligned text entry, otherwise latin character input,
// like "abc123", will jump to the left and may even disappear!
password.setTextDirection(View.TEXT_DIRECTION_RTL);
// Make the "Enter password" hint display on the right hand side
password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
}
password.addTextChangedListener(new TextWatcher() {
boolean inputTypeChanged;
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void afterTextChanged(Editable s) {
// Workaround https://code.google.com/p/android/issues/detail?id=201471 for Android 4.4+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && isRTL(getActivity())) {
if (s.length() > 0) {
if (!inputTypeChanged) {
// When a character is typed, dynamically change the EditText's
// InputType to PASSWORD, to show the dots and conceal the typed characters.
password.setInputType(InputType.TYPE_CLASS_TEXT |
InputType.TYPE_TEXT_VARIATION_PASSWORD |
InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
// Move the cursor to the correct place (after the typed character)
password.setSelection(s.length());
inputTypeChanged = true;
}
} else {
// Reset EditText: Make the "Enter password" hint display on the right
password.setInputType(InputType.TYPE_CLASS_TEXT |
InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
inputTypeChanged = false;
}
}
}
});
return view;
}
public static boolean isRTL(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
return context.getResources().getConfiguration().getLayoutDirection()
== View.LAYOUT_DIRECTION_RTL;
// Another way:
// Define a boolean resource as "true" in res/values-ldrtl
// and "false" in res/values
// return context.getResources().getBoolean(R.bool.is_right_to_left);
} else {
return false;
}
}
呼叫需要
void print(char *name)
{
puts(name);
}
print(t1.name);
需要puts
(或实际为char *
)。
t1.name的数据类型为const char *
同样
char *
和电话
void print1(struct test id, char *name)
{
printf("\n%d\n",id);
puts(name);
}
数组的名称退化为数组的第一个元素的地址。因此,当print1(t1.id,& t1.name);
传递给函数时,它将成为char数组的起始地址。