这段代码产生"p = hello world":
#include "stdio.h"
#include "string.h"
int main(){
char *p;
p="hello world";
printf("p is %s \n",p);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是这段代码产生了一个分段错误:
#include "stdio.h"
#include "string.h"
int main() {
char *p;
strcpy(p,"hello");
printf("p is %s \n",p);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这段代码产生"p = hello"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
int main() {
char *p;
p=(char*)malloc (10);
strcpy(p,"hello");
printf("p is %s \n",p);
return 0;
Run Code Online (Sandbox Code Playgroud)
}
可能重复:
静态变量
如何从C中的另一个文件访问静态变量?由于静态变量具有文件范围,我认为我们无法在文件外访问它.但我仍然觉得可能有一些技巧或方法来做同样的事情.
在vxworks中,是否应该使用VX_FP_TASK选项生成每个任务?
如果您的任务使用任何浮点运算,则需要VX_FP_TASK选项.但是,如何预测未来 - 我的意思是,如何知道他/她是否会使用浮动?
在修复任何错误或引入新代码的同时,程序员是否应该找到所有任务将受到他/她的代码特征影响,以及该任务是否使用此选项生成?这非常乏味.我错过了什么吗?
我想为我的学习实现一个小的路由表?我知道它是在路由器中使用radix/patricia树实现的吗?
有人可以给我一个如何实现相同的想法吗?
我觉得的主要问题是存储IP ADDRESS.例如:10.1.1.0网络下一跳20.1.1.1 10.1.0.0网络下一跳40.1.1.1
有人能给我一个结构的声明,我可以从中得到一个想法吗?
我的一些C程序没有按预期工作.例如,在C中不能通过引用传递,但是当我编写一个使用它并使用gcc编译它的C程序时,它工作正常.
gcc是C++编译器吗?如何使其行为像C编译器?
代码:
#include "stdio.h"
#include "string.h"
int main()
{
char *p = "abc";
printf("p is %s \n", p);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
p is abc
代码:
#include "stdio.h"
#include "string.h"
int main()
{
char *p = "abc";
strcpy(p, "def");
printf("p is %s \n",p);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
Segmentation fault (core dumped)
有人能解释为什么会这样吗?
我有一个关于哈希表实现的C编程问题.我已经实现了哈希表来存储一些字符串.我在处理哈希冲突时遇到了问题.我遵循链接链接列表方法来克服这个问题,但不知何故,我的代码表现不同.我无法调试它.有人可以帮忙吗?
这就是我所面对的:第一次说,我插入一个名为gaur的字符串.我的哈希映射计算索引为0并成功插入字符串.但是,当另一个字符串的哈希值在计算时,结果为0时,我的先前值被覆盖,即gaur将被新字符串替换.
这是我的代码:
struct list
{
char *string;
struct list *next;
};
struct hash_table
{
int size; /* the size of the table */
struct list **table; /* the table elements */
};
struct hash_table *create_hash_table(int size)
{
struct hash_table *new_table;
int i;
if (size<1) return NULL; /* invalid size for table */
/* Attempt to allocate memory for the table structure */
if ((new_table = malloc(sizeof(struct hash_table))) == NULL) {
return NULL;
}
/* Attempt to allocate memory for …Run Code Online (Sandbox Code Playgroud) 可能重复:
在C下通过引用传递指针参数?
参考变量是C++概念吗?它们是否可用于C?如果在C中可用,为什么我的代码会出现编译错误?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a = 10;
int b = 20;
int &c = a;
int &d = b;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
bash-3.2$ gcc test.c
test.c: In function `main':
test.c:12: error: parse error before '&' token
Run Code Online (Sandbox Code Playgroud)
为什么?
我有一个C编程问题.
我想知道两者之间的差异,以及一个对其他人有用的地方吗?
假设我有一个名为employee的结构,如下所示:
struct emp{
char first_name[10];
char last_name[10];
char key[10];
};
Run Code Online (Sandbox Code Playgroud)
现在,我想存储员工记录表,然后我应该使用哪种方法:
struct emp e1[100]; // Orstruct emp * e1[100];我知道这两个不一样,但是想知道第二个声明会引起兴趣并且更有利于使用的用例.
有人可以澄清吗?
我怎么能从另一个文件访问静态变量?静态变量不具有文件范围吗?
bash-3.2$ ls
a.c b.c
bash-3.2$ cat a.c
#include <stdio.h>
static int s = 100;
int fn()
{
/* some code */
}
bash-3.2$ cat b.c
#include <stdio.h>
#include "a.c"
extern int s;
int main()
{
printf("s = %d \n",s);
return 0;
}
bash-3.2$ gcc b.c
bash-3.2$ a.exe
s = 100
Run Code Online (Sandbox Code Playgroud) 这是我的test.cpp:
#include <iostream.h>
class C {
public:
C();
~C();
};
int main()
{
C obj;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我使用该命令编译它时g++ test.cpp,我收到此错误消息:
In file included from /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/backward/iostream.h:31,
from test.cpp:1:
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the header for the header for C++ includes, or instead of the deprecated header . To disable … 在这个散列例程中:1.)我能够添加字符串.2.)我能够查看我添加的字符串.3.)当我尝试添加一个重复的字符串时,它会抛出一个错误,表示已存在.4.)但是,当我尝试删除已经存在于哈希表中的相同字符串时,则lookup_routine调用哈希函数来获取索引.此时,它会向已添加的哈希索引抛出不同的哈希索引.因此,我的删除例程失败了吗?
我能够理解为什么相同的字符串,哈希函数每次计算不同的索引(而相同的逻辑在视图哈希表例程中工作)?有人能帮我吗?
$ ./a
Press 1 to add an element to the hashtable
Press 2 to delete an element from the hashtable
Press 3 to search the hashtable
Press 4 to view the hashtable
Press 5 to exit
Please enter your choice: 1
Please enter the string :gaura
enters in add_string
DEBUG purpose in hash function:
str passed = gaura
Hashval returned in hash func= 1
hashval = 1
enters in lookup_string
str in lookup_string = gaura
DEBUG purpose …Run Code Online (Sandbox Code Playgroud)