在C++中,我需要:
main()它,它显示原始数组与新反转的字符.我在创建实际执行倒车的功能时遇到了麻烦,因为我有一些限制:
我的函数只传入原始数组,即:
void reverse(char word[])
Run Code Online (Sandbox Code Playgroud)
编辑:到目前为止这是我的代码库:
void reverse(char word[]);
void main()
{
char word[MAX_SIZE];
cout << endl << "Enter a word : ";
cin >> word;
cout << "You entered the word " << word << endl;
reverse(word);
cout << "The word in reverse order is " << word << endl;
}
void reverse(char myword[])
{
int i, temp;
j--;
for(i=0;i<(j/2);i++)
{
temp = myword[i];
myword[i] = myword[j];
myword[j] = …Run Code Online (Sandbox Code Playgroud) 我读了这本关于GCC的官方手册.有时我翻译文本有问题.在第六页(第2.1章)我无法理解这样的文本片段:
ISO C标准定义了(在第4节中)两类符合实现.符合标准的托管实现支持整个标准,包括所有图书馆设施; 一个符合独立的实现只需要提供一定的图书馆设施:那些
<float.h>,<limits.h>,<stdarg.h>,和<stddef.h>; 从AMD1开始,也是那些<iso646.h>; 自C99以来,也是那些<stdbool.h>和<stdint.h>; 从C11开始,也是那些<stdalign.h>和.此外,C99中添加的复杂类型不是独立实现所必需的.该标准还为程序定义了两个环境,一个独立的环境,所有实现都需要,并且可能没有超出独立实现所需的库设施,其中程序启动和终止的处理是实现定义的,以及托管环境,不是必需的,其中提供了所有的图书馆设施,并通过功能int main (void)或启动int main (int, char *[]).操作系统内核将是一个独立的环境; 使用操作系统功能的程序通常位于托管实现中.
我不确定我是否理解正确...
我将改述我的理解:
轻型版本用于OS开发.完整版本适用于可在OS中运行的程序.
我不理解关于main功能的短语.
我要求向我解释这段文字.
在尝试理解更大的问题时,我正在进行一些测试.这是我的测试环境:
head.h:
#define MAX_BUFSIZE 500
typedef struct {
int head;
int tail;
int status;
int active;
void * dev[MAX_BUFSIZE];
char free[MAX_BUFSIZE];
int count;
} msg_fifo_t;
extern msg_fifo_t TxBufx[];
extern msg_fifo_t Rx_Buf[];
Run Code Online (Sandbox Code Playgroud)
test.c:
#include <stdio.h>
#include "head.h"
//msg_fifo_t TxBufx[10]; // This is the important line
int main(int argc, char * argv[])
{
// This part isn't really important...
printf("Hello Test\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
所以我使用这些文件并运行了三个测试来查看我得到的尺寸 -
测试#1(代码如上):
> gcc -Os test.c
> ls -al a.out
-rwxrwxr-x 1 mike …Run Code Online (Sandbox Code Playgroud) 我正在玩弄一些打开,阅读和修改文本文件的代码.一个快速(简化)的例子是:
#include <stdio.h>
int main()
{
FILE * fp = fopen("test.txt", "r+");
char line[100] = {'\0'};
int count = 0;
int ret_code = 0;
while(!feof(fp)){
fgets(line, 100, fp);
// do some processing on line...
count++;
if(count == 4) {
ret_code = fprintf(fp, "replaced this line\n");
printf("ret code was %d\n", ret_code);
perror("Error was: ");
}
}
fclose(fp);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在在Linux上,使用gcc(4.6.2)编译此代码,并修改文件的第5行.在使用Visual C++ 2010编译的Windows7上运行的相同代码运行并声称已成功(报告返回代码为19个字符并perror说"无错误")但无法替换该行.
在Linux上,我的文件具有完全权限:
-rw-rw-rw- 1 mike users 191 Feb 14 10:11 test.txt
Run Code Online (Sandbox Code Playgroud)
据我所知,它在Windows上也是如此:
test.txt(右键单击) - >属性 - >安全性 …
我正在写一个函数来返回一个数字的反转,即它转换int(1234)为int(4321).这就是我目前所拥有的:
#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>
using namespace std;
int reverse(int num) {
stringstream ss (stringstream::in | stringstream::out);
string initial;
int reversed;
// read the number in to a string stream
ss << num;
initial = ss.str();
// flush the stringstream
ss.str("");
for(unsigned int i(0); i <= initial.size(); i++) {
ss << initial[initial.size() - i];
}
ss >> reversed;
return reversed;
}
int main(int argc, const char *argv[])
{
int test = 9871; …Run Code Online (Sandbox Code Playgroud) 我在桌面上运行了一个linux应用程序,我想将syslog()调用重定向到printf()调用.
注:我不希望更换电话,只是重定向
所以我写了一些代码来做到这一点:
#ifndef EMBED
#define syslog(level, stuff) printf("SYSLOG: %s\n", stuff)
#endif
Run Code Online (Sandbox Code Playgroud)
在我使用它的一个文件中工作得很好.我将其移动到一个新文件并收到错误:
error: macro "syslog" passed 3 arguments, but takes just 2
Run Code Online (Sandbox Code Playgroud)
我知道错误是因为新文件中的调用是混合的,有些是使用2个参数连接到syslog,有些是使用3.我也知道我需要以某种方式通过变量参数列表重定向这个,但我该怎么做?我还没有工作呢......
据我了解,syslog()和printf()应该是:
void syslog(int priority, const char *format, ...)
int printf(const char *format, ...)
Run Code Online (Sandbox Code Playgroud)
所以我尝试过:
#define ERR 3
#ifndef EMBED // This is not defined in my env, btw
#define syslog(pri, fmt, ...) printf(fmt, ...)
#endif
...
void main() {
...
syslog(ERR, "test");
Run Code Online (Sandbox Code Playgroud)
但是这给出了错误:
error: expected expression before ‘...’ token
Run Code Online (Sandbox Code Playgroud)
关于如何使用这个宏的建议?
我的机器上有一个C/Python设置,我正在用串行通信进行一些测试,出于某种原因,我从来没有读过超过1个字节.
我的设置:我有一台Windows 7机器,在虚拟机箱中运行OpenSUSE.我有2个USB-RS232转换器和它们之间的适配器(所以它是从一个USB端口到另一个的循环).
在Windows方面,我能够让他们通过Python-to-Python和C-to-Python相互通信.一旦我使用Linux VM,我就可以从C(Linux)到Python(Windows)获取数据,但是当我这样做时,我只能得到1个字节.我认为我打开文件或执行Linux C代码上的读取有问题,但我不确定可能是什么问题.
Python代码(使用PySerial):
>>> import serial
>>> ser = serial.Serial(3)
>>> ser
Serial<id=0x2491780, open=True>(port='COM4', baudrate=9600, bytesize=8,
parity='N', stopbits=1, timeout=None, xonxoff=False, rtscts=False, dsrdtr=False)
>>> ser.read(5)
'Hello'
>>> ser.write("hi you")
6L
Run Code Online (Sandbox Code Playgroud)
C代码:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
int open_port()
{
int fd;
fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
if(fd < 0)
perror("open_port: Unable to open /dev/ttyUSB0 - ");
else
fcntl(fd, F_SETFL, 0);
return fd;
}
int swrite(int fd, char …Run Code Online (Sandbox Code Playgroud) 您好我是编程新手,我正在用C编写程序.
在我的头文件中,我有这个宏:
#define yesno(c) (c==ENTER || c==' ' || c=='\t') ? ENTER : ESC
Run Code Online (Sandbox Code Playgroud)
在我的程序中,我有这个代码
char keypressed()
{ char c;
c =getch();
return yesno(getch());
}
Run Code Online (Sandbox Code Playgroud)
所以我想问的是为什么当我要求返回时yesno(c)我只需按一次按钮,而当我使用返回时yesno(getch())我必须再按两次或三次按钮?
getch()从宏调用时有问题吗?
我用一个循环打印了这个模式:
*
**
***
****
*****
String s = "";
for (i = 1; i <= n; ++i) {
s += "*";
System.out.println(s);
}
Run Code Online (Sandbox Code Playgroud)
现在我想要如何仅使用一个循环打印以下模式.
1)
*
* *
* * *
* * * *
* * * * *
2)
* * * * *
* * * *
* * *
* *
*
3)
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Run Code Online (Sandbox Code Playgroud)
和其他类似的模式只使用一个循环,我已经完成了所有使用多个循环.
我有一个Python列表,我可以获取它的指针,并将此指针地址传递给C++进行处理
MyPointer = TheList.as_pointer()
Run Code Online (Sandbox Code Playgroud)
现在我用ctypes将这个地址传递给C++
在C++中,我可以执行以下操作:
*(float*) MyPointer = 2.0f; //for example
Run Code Online (Sandbox Code Playgroud)
并且Python值会立即更新,现在的问题是:如何扩展或删除一些值(比如直接从C++修改列表),因为我感觉这些数据是std::vector如何做的push_back等等以快速方式调整大小(在Python中迭代几乎是缓慢的)