小编joh*_*han的帖子

strcpy vs strdup

我读到的strcpy是复制字符串,并strdup返回指向新字符串的指针以复制字符串.

您能否解释一下您喜欢使用strcpy哪些情况以及您更喜欢使用哪些情况strdup

c strdup strcpy

65
推荐指数
4
解决办法
7万
查看次数

将两个uint8_t组合为uint16_t

我有以下数据

uint8_t d1=0x01; 
uint8_t d2=0x02; 
Run Code Online (Sandbox Code Playgroud)

我想将它们合并为uint16_t

uint16_t wd = 0x0201;
Run Code Online (Sandbox Code Playgroud)

我该怎么做?

c

20
推荐指数
3
解决办法
3万
查看次数

C中的精确计时

我在下面有一些代码.我使用此代码unsigned output[38]从嵌入式板的GPIO 输出1s和0s().

我的问题:两个输出值(1,0或0,1)之间的时间应该是416微秒,正如我在clock_nanosleep下面的代码中定义的那样,我也用于sched_priority()更好的时间分辨率.但是,示波器(下图)测量显示两个输出值之间的时间是770 usec.我想知道为什么信号之间有那么多的不准确?

PS.主板(beagleboard)有Linux 3.2.0-23-omap#36-Ubuntu Tue Apr 10 20:24:21 UTC 2012 armv7l armv7l armv7l GNU/Linux内核,它有750 MHz CPU,top几乎没有显示CPU(~1在运行我的代码之前消耗了%)和内存(~0.5%).我使用没有校准问题的电子示波器.

#include <stdio.h>
#include <stdlib.h> //exit();
#include <sched.h>
#include <time.h>

void msg_send();
struct sched_param sp;

int main(void){
      sp.sched_priority = sched_get_priority_max(SCHED_FIFO);
      sched_setscheduler(0, SCHED_FIFO, &sp);
      msg_send();
    return 0;
}

void msg_send(){
    unsigned output[38] = {0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,1}; 

    FILE *fp8;
    if ((fp8 = fopen("/sys/class/gpio/export", "w")) == NULL){  //echo 139 > export
           fprintf(stderr,"Cannot open export file: …
Run Code Online (Sandbox Code Playgroud)

c linux time

10
推荐指数
1
解决办法
1847
查看次数

在C中定义未使用的参数

我需要使用pthreat但我不需要将任何参数传递给函数.因此,我将NULL传递给pthread_create上的函数.我有7个pthreads,所以gcc编译器警告我有7个未完成的参数.如何在C编程中将这7个参数定义为未使用?如果我没有将这些参数定义为未使用,是否会导致任何问题?提前感谢您的回复.

void *timer1_function(void * parameter1){
//<statement>
}

int main(int argc,char *argv[]){
  int thread_check1;
  pthread_t timer1;
  thread_check1 = pthread_create( &timer1, NULL, timer1_function,  NULL);
    if(thread_check1 !=0){
        perror("thread creation failed");
        exit(EXIT_FAILURE);
    }
while(1){}
return 0;
}
Run Code Online (Sandbox Code Playgroud)

c gcc posix pthreads

8
推荐指数
2
解决办法
1万
查看次数

ad-hoc wlan的预定义小区ID分配

我有一个debian/ubuntu板,我通过ad-hoc网络连接它们,具有以下设置

board:~# cat /etc/network/interfaces
auto wlan0
iface wlan0 inet static
        address 10.0.0.2
        netmask 255.255.255.0
        wpa-driver nl80211
        wpa-conf /etc/wpa_supplicant.conf

board:~# cat /etc/wpa_supplicant.conf
# IBSS/ad-hoc network with WPA-None/TKIP.
ctrl_interface=/var/run/wpa_supplicant
ap_scan=1

network={
        ssid="adhoc_test"
        mode=1
        frequency=2412
        proto=WPA
        key_mgmt=WPA-NONE
        pairwise=NONE
        group=TKIP
        psk="abcdefgh"
        bssid=F8:D1:11:52:0C:4E
}
Run Code Online (Sandbox Code Playgroud)

这些配置会创建一个ad-hoc wlan,但是,节点通常会获得不同的小区ID,因此它们无法相互通信.为了防止它,我添加了bssid=F8:D1:11:52:0C:4E行,但是,当获取单元ID时,没有节点获得此预定义的单元ID.

我的问题是如何防止节点获取不同的小区ID?为什么bssidline在ad-hoc模式下不起作用?

PS我在ubuntu和debian dist上尝试了这些设置.有3.2和3.4内核.我使用的wpa_supplicant版本是0.7,1.0和2.0都不起作用.对于芯片组,我使用Atheros AR9271芯片组作为wifi模块.

linux wireless adhoc wireless-connection

6
推荐指数
2
解决办法
1551
查看次数

atoi从定义的索引转换为字符串到字符串

假设我有char x[3] = "123";,我想只转换23char数组的索引1和index2" ",我能做到atoi吗?

我知道我可以做到,char z[2]; z[0]=x[1]; z[1]=x[2]; atoi(z);但这不是我要求的.

c linux atoi

3
推荐指数
1
解决办法
1678
查看次数

C中的struct数组初始化

这是我的代码的一部分.我想初始化arraylist[0]as arraylist[0].x = 0arraylist[0].y = 0.我不需要初始化struct数组的其余部分.我该怎么做?谢谢.

#include <stdio.h>
struct example {
    int x;
    int y;
};
struct example arraylist[40];

int main(int argc, char *argv[]){
    printf("%d\n %d\n", arraylist[0].x, arraylist[0].y);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c arrays struct initialization

2
推荐指数
1
解决办法
5202
查看次数

如何在C中将char数组转换为整数?

如何在下面的代码中将char数组转换为x整数89?谢谢

int main(int argc,char *argv[]){
    char y[13] = "0123456789012";
    char x[3];
    int integer_value;

    x[0] = y[8];
    x[1] = y[9];
    x[3] = '\0';

    integer_value=atoi(x);
}
Run Code Online (Sandbox Code Playgroud)

c string type-conversion char

2
推荐指数
1
解决办法
2万
查看次数

为什么在linux编程中使用system()很糟糕?

可能重复:
从C,C++在Linux中发出系统命令

我在一些文字上看到在linux编程中使用system()调用并不好,我想知道它的真正原因是什么?它应该消耗更多的内存,也许更多的CPU.除了这些可能是什么原因?

例如,如果我输入system("echo 1 > file");而不是使用fopen(), fwrite()黑客在我的程序/ linux系统中可以做什么?我看到system()因安全问题而不建议.但是一个人如何因为使用system()调用而破解linux系统呢?如果有人能够明确地解释使用system()可能会变坏的话,我会很高兴.

c linux security

2
推荐指数
1
解决办法
8201
查看次数

如何在C中定义全局"struct"变量

我想定义一个由struct创建的全局/公共变量.我无法访问main或任何其他函数中的user_list.x或y/z.当我调试下面的代码时,我得到以下错误"请求成员'x'在某个结构或联合的东西中".如何声明可以从不同函数访问的全局结构变量?谢谢

#include <stdio.h>
#include <stdlib.h>

struct routing {
        int x;
        int y;
        int z;
};
struct routing user_list[40];

int main(int argc,char *argv[])
{

    user_list.x = 1;
    printf("user_list.x is %d",user_list.x);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c struct global

1
推荐指数
1
解决办法
1万
查看次数

p编程中的pthread内存泄漏

我有下面的代码.

void *timer1_function(void * eit);
pthread_t timer1;
int thread_check1 = 0;

line72: thread_check1 = pthread_create( &timer1, NULL, timer1_function,  NULL);
Run Code Online (Sandbox Code Playgroud)

Valgrind显示下面的输出,并表示存在问题line 72.pthread_create上面的用法有什么问题?

272 bytes in 1 blocks are possibly lost in loss record 2 of 5
  in main in main.c:72
  1: calloc in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so
  2: _dl_allocate_tls in /build/buildd/eglibc-2.15/elf/dl-tls.c:297
  3: pthread_create@@GLIBC_2.2.5 in /build/buildd/eglibc-2.15/nptl/allocatestack.c:571
  4: main in <a href="file:///home/user/Project-build-desktop-Qt_4_8_1_in_PATH__System__Release/../project/main.c:72" >main.c:72</a>
Run Code Online (Sandbox Code Playgroud)

c memory-leaks pthreads

1
推荐指数
1
解决办法
1939
查看次数

错误:尝试使用中毒的“ SIG_OUTPUT_COMPARE0A”

我正在为atmega168a编写C代码。我attempt to use poisoned "SIG_OUTPUT_COMPARE0Aattempt to use poisoned "SIG_OUTPUT_COMPARE0B我编译下面的代码错误。但是我ISRhttp://www.protostack.com/blog/2010/09/timer-interrupts-on-an-atmega168/上看到了类似的功能用法,如果有人可以告诉我我做错了什么,我将不胜感激。我正在使用atmel studio 6.0及其gcc在Windows 7 pc上编译代码。

#include <avr/io.h>
#include <avr/interrupt.h> 
#include <avr/iomx8.h>


volatile int new_msg;
volatile int new_msg_available = 0;
volatile int message = 0xFFFF;
int main(void)
{
    DDRB = 0xFF;
    TIMSK0 = _BV(OCIE0A) | _BV(OCIE0B);  // Enable Interrupt TimerCounter0 Compare Match A & B   // #define OCIE0A  1... (1<<1);
    TCCR0A = _BV(WGM01);  // Mode = CTC ... #define WGM01   1 ... mode 2
    TCCR0B …
Run Code Online (Sandbox Code Playgroud)

c gcc avr atmega atmel

1
推荐指数
1
解决办法
5959
查看次数

在C - linux上的system()上执行命令

我运行下面的代码,

int main() {
  char settime_parameters[13]= "042122142013";
  char command[25];
  sprintf(command, "date %s", settime_parameters );
  printf("%s\n",command);
  system("commad");
}
Run Code Online (Sandbox Code Playgroud)

我得到这个输出:

date 042122142013
sh: 1: commad: not found
Run Code Online (Sandbox Code Playgroud)

但是,如果我date 042122142013在终端上运行,它工作正常并改变系统时间.我想知道为什么当我通过它执行它时它不起作用system()

谢谢.

c linux datetime posix

0
推荐指数
1
解决办法
867
查看次数