在StackOverflow上找不到任何可解决此问题的问题。
我意识到char *数组不必以NULL终止,但是想知道什么时候需要它?
例如,在调试代码时,我使用很多printf()来查看我的变量在代码的某些阶段是否正确。
我有一个char **值,可容纳4个char *,最后一个char *为NULL。使用NULL终止时,从值[0]到值[3]的printfs会给我这 一点:名称只是我完成打印值数组后立即打印的另一个数组
Testing values1[0]: %HOME/bin:%PATH
Testing values1[1]: /%HOME/include
Testing values1[2]: /%HOME/lib
Testing values1[3]: (null)
Testing names2[0]: PATH
Testing names2[1]: IDIR
Testing names2[2]: LIBDIR
Run Code Online (Sandbox Code Playgroud)
我有一个char **和3个char *,所有这些都是有效的char *。如果不使用NULL终止,则printf从values [0]到values [3]给了我(名称未显示)
Testing values1[0]: %HOME/bin:%PATH
Testing values1[1]: /%HOME/include
Testing values1[2]: /%HOME/lib
Run Code Online (Sandbox Code Playgroud)
我认为,当printf(....,values [3])将是未定义的行为,例如打印垃圾值,但是如上面的输出所示,包括printf(....,values [3]以及之后的所有内容])似乎尚未执行。
看看这段代码:
int main() {
int foo = 0;
const int *ptr = &foo;
*ptr = 1; // <-- Error here
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译时,clang给了我一个错误:
const.c:5:7: error: read-only variable is not assignable
Run Code Online (Sandbox Code Playgroud)
是的,ptr是const,但foo不是.为什么我不能分配值foo?
为什么我必须在code函数内部的以下示例中对指针进行类型转换才能返回类型的值,char因为我已经在函数定义中将其声明为char的常量指针?
特别cout << (*p + 1)是结果是整数格式,而当更改为时,cout << (char) (*p + 1)结果以char格式显示,因为我对它进行了类型转换.
<<操作员是否有一些关于要显示的类型的默认参数?
#include <iostream>
using namespace std;
void code(const char* p);
int main()
{
code("This is a test");
return 0;
}
void code(const char* p)
{
while(*p)
{
cout << (*p + 1);
p++;
}
}
Run Code Online (Sandbox Code Playgroud) 我想为这个容器分配一个size_t:
std::vector <nts::Tristate *> _components;
Run Code Online (Sandbox Code Playgroud)
要做到这一点,我想投const size_t &给nts::Tristate *
this->_components[0] = static_cast<nts::Tristate *>(&value);
Run Code Online (Sandbox Code Playgroud)
但是我有以下错误:
error: invalid static_cast from type ‘const size_t* {aka const long unsigned int*}’ to type ‘nts::Tristate*’
this->_components[0] = static_cast<nts::Tristate *>(&value);
Run Code Online (Sandbox Code Playgroud)
知道为什么吗?
我有一个节点,我正在定义其全局指针变量,如下所示:
typedef struct node
{
char* word;
struct node* next;
} node;
node* HashTable = NULL;
node* HeadOfHashTable = NULL;
Run Code Online (Sandbox Code Playgroud)
现在,我分配了如下内存:
void allocateMemory(int numOfElements, bool isRealloc, const char* word)
{
if(!isRealloc)
{
printf("Allocating %d blocks\n", numOfElements);
HashTable = malloc(sizeof(node*) * numOfElements);
} else {
printf("Reallocating %d blocks for %s", numOfElements, word);
HashTable = realloc(HashTable, sizeof(node*) * numOfElements);
}
if(HashTable == NULL)
{
printf("### Out Of Memory ###\n");
exit(0);
}
HeadOfHashTable = HashTable;
}
Run Code Online (Sandbox Code Playgroud)
现在,我正在传递一个HASH值和单词以放入哈希表,在下面的方法中.我已经评论了我在哪里遇到了seg错误.
void putInHashTable(char* ch, unsigned int hashValue) …Run Code Online (Sandbox Code Playgroud) 我的readfile方法有问题.我无法在文本文件中打印出数字值,但我的名字很好.
这就是我的文本文件中的所有内容:
Bob
10
12.00
Run Code Online (Sandbox Code Playgroud)
码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "iofunctions.h"
int readfile( struct account accarray[], int* numcust, char filename[])
{
/* variables */
FILE *inputFile;
char line[25];
char spaceEater[25];
char name[25]={"\0"};
int accountNum;
float money ;
inputFile = fopen("People_Info.txt", "r");
if(inputFile == NULL)
{
fprintf(stderr, "Sorry, I cannot open this file. Also it's empty.\n");
exit(1);
}
/*outer loop is to check if file is not null*/
while(!feof(inputFile))
{
/*while file has something*/
while(fscanf(inputFile,"%s %d …Run Code Online (Sandbox Code Playgroud) 我正在为C中的程序编写一个简单的函数,它将打印一个用法语句并在调用时退出该程序.我希望有一个选项来传递一个字符串作为参数并显示它,但也没有传递任何内容并跳过该行.我的代码如下,主要功能如下:
#include <stdlib.h>
#include <stdio.h>
typedef struct opt_info
{
char *start_path; /* Path to directory to start find in */
int name_flag; /* flag for the name argument */
int type_flag; /* flag for the type argument */
} opt_info;
/* function declarations */
int process_args( opt_info *opts, int ac, char *av[] );
void init_params( opt_info *opts );
void print_usage();
int main ( int ac, char *av[] )
{
opt_info opts; /* struct to store run options */
process_args( &opts, …Run Code Online (Sandbox Code Playgroud) 我编写了一个程序来反转字符串,其中字符串是一个字符数组.该计划是:
#include<iostream>
void rev(char *str)
{
char *end;
end = str;// end will now point to the same address of str
// now we need to incremet the end pointer upto it encounter null
if(str)// this checks if the string is present or not
{
while(*end)
++end;
--end;// set one character back, since last character is null
// swap characters from start of string with the end of the string
// until the pointers meet in middle.
while(str < end) …Run Code Online (Sandbox Code Playgroud) 在这个示例代码中,while循环后指针真的可以无效,并且在编写代码时是否应该考虑到这一点?或者C标准是否被误解和/或有缺陷?
#include <stdio.h>
int main(int argc, char **argv) {
(void)argc;
(void)argv;
int *pointer;
int object[1];
pointer = object;
printf("pointer -before: %p\n", (void*)pointer);
do {
int other_object[1];
printf("a pointer \"just past\" other_object can look like: %p\n", (void*)(&other_object+1));
printf("address of other_object: %p\n", (void*)&other_object);
} while (0);
puts("the lifetime of other_object has ended");
printf("pointer -after: %p\n", (void*)pointer);
}
Run Code Online (Sandbox Code Playgroud)
可能的输出(在我的机器上运行):
pointer -before: 0x7fff5f744ae4
a pointer "just past" other_object can look like: 0x7fff5f744ae4
address of other_object: 0x7fff5f744ae0
the lifetime of other_object has ended
pointer -after: …Run Code Online (Sandbox Code Playgroud) 根据标题.我看到某些人宣称指针为char* s,而其他人则将其声明为char *s.我也看到有些人在做,s = (char*) malloc(5)而不是做*s = malloc(5).那之间有什么区别吗?抱歉,如果重复这个问题,我理解指针的概念,但很难理解用于表示C中指针的语法.