C. Passing pointers to be modified causing segmentation faults

I am making a decimal to binary converter in C for a class. I want to pass a char array to my function as well as the the decimal as an int. ie void DtoB(int decimal, char *array); DtoB will do the math and modify array to be the binary value. Ideally by giving it int values. Main() will just scanf(decimal), DtoB(decimal,array), and printf(array). Here is what I have. This just returns a

C.传递要修改的指针导致段错误

我正在C中为一个类制作一个十进制的二进制转换器。 我想通过一个字符数组到我的函数以及作为int的小数。 即无效DtoB(int十进制,字符*数组); DtoB将执行数学运算并将数组修改为二进制值。 理想的是通过给它int值。 Main()将只扫描f(十进制),DtoB(十进制,数组)和printf(数组)。 这是我的。 这只是返回一个分段错误 1 #include <stdio.h> 2 #include <math.h> 3 4 void DecToBin(unsigned int, ch

Modify the content of char* str

I'm trying to reverse the string via void function, but segmentation fault occurs for "str" it's stored read-only memory (as I known from searching existing thread in the forum). I tried to use strcpy(copy, str) in "reverse" function but it's just reversing the newly created char array "copy" which cannot be returned to main function. I don't want

修改char * str的内容

我尝试通过void函数来反转字符串,但是对于“str”它存储的只读内存(正如我在搜索论坛中的现有线程中所知的)发生分段错误。 我试图在“反向”函数中使用strcpy(copy,str),但它只是反转新创建的char数组“copy”,它不能返回到main函数。 我不想在反向函数中使用printf(“%s”,copy)来打印反转的字符串。 无论如何反向str 不改变它为char str[] = "Hello" 不改变main()函数中的代码 将reverse()的函数类型

C Text Processing: Output Wrong Size

I'm writing ac program that takes a text file of words, and copies only words without capitalization or punctuation, and which are 4 or more characters long. I have tested the boolean functions int containsPunctuationOrCaps(char *word) and int longerThanThree(char *word) , and they both work. However, my main function only prints words of at least seven characters, and anything longer is tr

C文本处理:输出大小错误

我正在写一个需要文本文件的ac程序,并且只复制没有大小写或标点符号,并且长度为4个或更多字符的单词。 我测试了布尔函数int containsPunctuationOrCaps(char * word)和int longerThanThree(char * word),它们都工作。 但是,我的主要功能只打印至少七个字符的单词,并且任何更长的内容都会被截断。 int main() { char *currentWord = malloc(36); int count = 0; char *Words[3000]; FILE *fin, *fout; fin

call printf using va

void TestPrint(char* format, ...) { va_list argList; va_start(argList, format); printf(format, argList); va_end(argList); } int main() { TestPrint("Test print %s %dn", "string", 55); return 0; } I neet to get: Test print string 55 Actually, I get garbage output. What is wrong in this code? 改用vprintf() 。 Instead of printf, I recommend you try vprintf instead, wh

使用va调用printf

void TestPrint(char* format, ...) { va_list argList; va_start(argList, format); printf(format, argList); va_end(argList); } int main() { TestPrint("Test print %s %dn", "string", 55); return 0; } 我需要得到: Test print string 55 其实,我得到垃圾输出。 这段代码有什么问题? 改用vprintf() 。 我建议你不要使用printf,而应该尝试使用vprintf,它是为了这个特定目的而创建的

Program run in child process doesn't loop

I have a specific question regarding the parent process reading the stdout from child. My problem is that when I run the program, the child program should execute a new program multiple times in a loop but it runs it only once and exits to the parent process. The child process is running a simple program that prints a message to stdout. Thanks in advance. #include <sys/types.h> #include

在子进程中运行的程序不会循环

我有一个关于父进程从孩子读取stdout的具体问题。 我的问题是,当我运行程序时,子程序应该在循环中多次执行一个新程序,但它只运行一次并退出到父进程。 子进程正在运行一个简单的程序,它向stdout输出一条消息。 提前致谢。 #include <sys/types.h> #include <stdio.h> #include <string.h> #include <unistd.h> #include <stdlib.h> #include <signal.h> #include <unistd.h> #include <assert

C: differences between char pointer and array

This question already has an answer here: Why do I get a segmentation fault when writing to a string initialized with “char *s” but not “char s[]”? 16 answers True, but it's a subtle difference. Essentially, the former: char amessage[] = "now is the time"; Defines an array whose members live in the current scope's stack space, whereas: char *pmessage = "now is the time"; Defines

C:char指针和数组之间的区别

这个问题在这里已经有了答案: 为什么在写入用“char * s”初始化但不是“char s []”的字符串时会出现分段错误? 16个答案 诚然,但这是一个微妙的差异。 基本上,前者: char amessage[] = "now is the time"; 定义一个数组,其成员位于当前作用域的堆栈空间中,而: char *pmessage = "now is the time"; 定义一个位于当前作用域堆栈空间的指针,但是引用其他地方的内存(在这里,“现在是时间”存储在其他地方,通常是一

Why sizeof of a struct is unsafe

The MSDN clearly states For all other types, including structs, the sizeof operator can only be used in unsafe code blocks. The C# Language Specification is even more precise : The order in which members are packed into a struct is unspecified. For alignment purposes, there may be unnamed padding at the beginning of a struct, within a struct, and at the end of the struct. The contents o

为什么sizeof结构是不安全的

MSDN明确指出 对于所有其他类型,包括结构体,sizeof运算符只能用于不安全的代码块。 C#语言规范更加精确: 未指定将成员打包到结构中的顺序。 出于对齐的目的,在结构的开始处,结构内以及结构的末端可能存在未命名的填充。 用作填充的位的内容是不确定的。 当应用于具有结构类型的操作数时,结果是该类型变量中的总字节数,包括任何填充。 但是,CLR如何处理以下结构: [StructLayout(LayoutKind.Explicit, Siz

sizeof() structures not known. Why?

Why can't I use sizeof() on simple structs? eg: private struct FloatShortPair { public float myFloat; public short myShort; }; int size = sizeof(FloatShortPair); //CS0233 error CS0233: 'FloatShortPair' does not have a predefined size, therefore sizeof can only be used in an unsafe context (consider using System.Runtime.InteropServices.Marshal.SizeOf) MSDN states: The

sizeof()结构未知。 为什么?

为什么我不能在简单结构上使用sizeof()? 例如: private struct FloatShortPair { public float myFloat; public short myShort; }; int size = sizeof(FloatShortPair); //CS0233 错误CS0233:'FloatShortPair'没有预定义的大小,因此sizeof只能用于不安全的上下文中(请考虑使用System.Runtime.InteropServices.Marshal.SizeOf) MSDN指出: sizeof运算符只能用于编译时常量的类型。 如果您收到

sizeof a struct member

This question already has an answer here: C: sizeof single struct member 9 answers sizeof(((struct A*)0)->arr); Briefly, cast a null pointer to a type of struct A* , but since the operand of sizeof is not evaluated, this is legal and allows you to get size of struct members without creating an instance of the struct. Basically, we are pretending that an instance of it exists at address 0

大小的结构成员

这个问题在这里已经有了答案: C:sizeof单个结构成员9个答案 sizeof(((struct A*)0)->arr); 简而言之,将空指针转换为struct A*类型的空指针,但由于sizeof的操作数未被计算,因此这是合法的,并且允许您在不创建结构实例的情况下获取struct成员的大小。 基本上,我们假装它的一个实例存在于地址0,并且可以用于offset和sizeof确定。 为了进一步阐述,请阅读这篇文章: http://www.embedded.com/design/prototypin

Regular expression for a string literal in flex/lex

I'm experimenting to learn flex and would like to match string literals. My code currently looks like: """([^n"\]*(\[.n])*)*""" {/*matches string-literal*/;} I've been struggling with variations for an hour or so and can't get it working the way it should. I'm essentially hoping to match a string literal that can't contain a new-line (unless it's escaped) and su

在flex / lex中的字符串文字的正则表达式

我正在尝试学习flex,并希望匹配字符串文字。 我的代码目前看起来像: """([^n"\]*(\[.n])*)*""" {/*matches string-literal*/;} 我一直在为一个小时左右的变化挣扎,无法按照它的方式工作。 我基本上希望匹配一个不能包含换行符的字符串文字(除非它已被转义)并支持转义字符。 我可能只是写了一个可怜的正则表达式,或者与flex不兼容。 请指教! 你会发现这些链接很有帮助 ANSI C语法,Lex规范 ANSI C Yac