我想从条目中设置var pid,但是我收到了以下警告:
gtk.c:7:6:警告:赋值在没有强制转换的情况下从指针生成整数[默认启用]。
如何制作var pid ==条目?这是非常不可取的,因为这个程序将向我们要进入的id(pid)进程发送信号。
#include <gtk/gtk.h>
#include <stdlib.h>
pid_t pid;
void sendSighup(GtkWidget *widget,GtkWidget *entry, gpointer label) {
pid = gtk_entry_get_text(GTK_ENTRY(entry));
kill(pid,SIGHUP);
}
void sendSigint(GtkWidget *widget, gpointer label) {
kill(pid,SIGINT);
}
void sendSigkill(GtkWidget *widget, gpointer label) {
kill(pid,SIGKILL);
}
void sendSigterm(GtkWidget *widget, gpointer label) {
kill(pid,SIGTERM);
}
void sendSigstop(GtkWidget *widget, gpointer label) {
kill(pid,SIGSTOP);
}
accept_clicked (GtkButton *button, GObject *context_object)
{
GtkLabel *accept_lable1 = g_object_get_data (context_object, "label1");
GtkEntry *accept_entry = g_object_get_data (context_object, "entry");
const char *entry_in = gtk_entry_get_text (accept_entry);
gtk_label_set_text (accept_lable1, entry_in);
}
int main( int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *frame;
GtkWidget *table;
GtkWidget *button1;
GtkWidget *button2;
GtkWidget *button3;
GtkWidget *button4;
GtkWidget *button5;
GtkWidget *quit;
GtkWidget *set;
GtkWidget *vseparator;
GtkWidget *entry;
GtkWidget *label;
GtkWidget *label1;
GtkWidget *label2;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Send Signal");
gtk_window_set_default_size(GTK_WINDOW(window), 300, 250);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
frame = gtk_fixed_new();
gtk_container_add(GTK_CONTAINER(window), frame);
button1 = gtk_button_new_with_label("Sighup");
gtk_fixed_put(GTK_FIXED(frame), button1, 8, 15);
gtk_widget_set_size_request(button1, 80, 35);
button2 = gtk_button_new_with_label("Sigint");
gtk_fixed_put(GTK_FIXED(frame), button2, 8, 60);
gtk_widget_set_size_request(button2, 80, 35);
button3 = gtk_button_new_with_label("Sigkill");
gtk_fixed_put(GTK_FIXED(frame), button3, 8, 105);
gtk_widget_set_size_request(button3, 80, 35);
button4 = gtk_button_new_with_label("Sigterm");
gtk_fixed_put(GTK_FIXED(frame), button4, 8, 150);
gtk_widget_set_size_request(button4, 80, 35);
button5 = gtk_button_new_with_label("Sigstop");
gtk_fixed_put(GTK_FIXED(frame), button5, 8, 195);
gtk_widget_set_size_request(button5, 80, 35);
set = gtk_button_new_with_label ("Set");
gtk_fixed_put(GTK_FIXED(frame), set, 200, 80);
gtk_widget_set_size_request(set, 80, 35);
quit = gtk_button_new_with_label("Quit");
gtk_fixed_put(GTK_FIXED(frame), quit, 200, 195);
gtk_widget_set_size_request(quit, 80, 35);
vseparator = gtk_vseparator_new();
gtk_widget_set_size_request(vseparator, 10, 240);
gtk_fixed_put(GTK_FIXED(frame), vseparator, 95, 5);
label = gtk_label_new("Enter PID:");
gtk_fixed_put(GTK_FIXED(frame), label, 165, 20);
label2 = gtk_label_new("PID:");
gtk_fixed_put(GTK_FIXED(frame), label2, 110, 89);
label1 = gtk_label_new("0000");
gtk_fixed_put(GTK_FIXED(frame), label1, 140, 89);
entry = gtk_entry_new();
gtk_fixed_put(GTK_FIXED(frame), entry, 120, 40);
gtk_widget_grab_focus(entry);
g_object_set_data(G_OBJECT(set), "label1", label1);
g_object_set_data(G_OBJECT(set), "entry", entry);
g_signal_connect(GTK_BUTTON (set), "clicked",
G_CALLBACK (accept_clicked), set);
g_signal_connect_swapped(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(button1, "clicked",
G_CALLBACK(sendSighup), NULL);
g_signal_connect(button2, "clicked",
G_CALLBACK(sendSigint), NULL);
g_signal_connect(button3, "clicked",
G_CALLBACK(sendSigkill), NULL);
g_signal_connect(button4, "clicked",
G_CALLBACK(sendSigterm), NULL);
g_signal_connect(button5, "clicked",
G_CALLBACK(sendSigstop), NULL);
g_signal_connect(G_OBJECT(quit), "clicked",
G_CALLBACK(gtk_main_quit), G_OBJECT(window));
gtk_widget_show_all(window);
gtk_widget_show(entry);
gtk_widget_show(label);
gtk_main();
return 0;
}
答案 0 :(得分:0)
答案 1 :(得分:0)
gtk_entry_get_text返回char * - 指向char的指针。您正在代码中为char指定一个指向int的指针。这是根据C标准实现定义的,可能不是你想要的,这就是你的编译器给你一个警告的原因:
6.3.2.3 p.6:
任何指针类型都可以转换为整数类型。除了 之前指定的,结果是实现定义的。如果 结果不能用整数类型表示,行为是 未定义。结果不必在任何值的范围内 整数类型。
例如,看看这段代码,它代表了您所遇到的问题:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char *p ="123456";
int i = p;
printf("%d\n", i);
printf("%p\n", p);
exit 0;
}
打印:
134514096
0x80485b0
指向char的指针在第7行中分配给int。这会产生与代码中相同的错误:
main.c:7:17:警告:初始化从指针生成整数 没有演员[默认启用]
现在让我们看看这个任务的结果是什么 - 不要忘记这是一个实现定义的行为。首先printf在赋值后打印i
变量的值。第二个printf使用%p
说明符打印指针保存的地址,该说明符以实现定义的方式打印指针值。这些值结果相同 - 0x80485b0
是十六进制134514096
。这里发生的是指针值(即地址)已分配给int。您的代码中也会发生同样的情况。 C是一种非常简单的语言,它不会背后做任何事情。它不会自动将字符串中写入的一系列数字转换为int。要将字符串转换为数字,您需要使用标准atoi函数或您正在使用的库提供的自定义函数。