小编alk*_*alk的帖子

指针数组的动态内存分配

我正在尝试编写一个程序,该程序从文本文件中读取一系列字符串,并将它们存储在字符串数组中,为每个元素动态分配内存.我的计划是使用指针将每个字符串存储在一个数组中,然后在读入更多数据时增大数组大小.我无法理解为什么我的测试代码无效.这是一个可行的想法吗?

char *aPtr;
aPtr =(char*)malloc(sizeof(char));

aPtr[0]="This is a test";


printf("%s",aPtr[0]);
Run Code Online (Sandbox Code Playgroud)

c arrays dynamic-memory-allocation pointer-to-pointer

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

Ruby 2.0.0 Array #bsearch行为

我注意到,从Ruby 2.0.0开始,数组类有一个bsearch我正在测试的方法,而且我没有得到我期望的行为.为什么它返回2和5的值,但是nil-1,1和4?

arr_in = [-1, 1, 2, 4, 5]

arr_in.bsearch { |x| x == 3 }   #=> nil
arr_in.bsearch { |x| x == -1 }  #=> nil
arr_in.bsearch { |x| x == 1 }   #=> nil
arr_in.bsearch { |x| x == 2 }   #=> 2
arr_in.bsearch { |x| x == 4 }   #=> nil
arr_in.bsearch { |x| x == 5 }   #=> 5
Run Code Online (Sandbox Code Playgroud)

ruby arrays binary-search bsearch

19
推荐指数
2
解决办法
3882
查看次数

我们应该在switch语句中打破默认情况吗?

假设这个示例代码(源代码):

#include <stdio.h>

void playgame()
{
    printf( "Play game called" );
}
void loadgame()
{
    printf( "Load game called" );
}
void playmultiplayer()
{
    printf( "Play multiplayer game called" );
}

int main()
{
    int input;

    printf( "1. Play game\n" );
    printf( "2. Load game\n" );
    printf( "3. Play multiplayer\n" );
    printf( "4. Exit\n" );
    printf( "Selection: " );
    scanf( "%d", &input );
    switch ( input ) {
        case 1:            /* Note the colon, not a semicolon */ …
Run Code Online (Sandbox Code Playgroud)

c break switch-statement

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

POSIX API调用列出进程中运行的所有pthread

我在POSIX/Linux环境中有一个多线程应用程序 - 我无法控制创建pthreads的代码.在某些时候,进程 - pthreads的所有者 - 收到一个信号.

该信号的处理程序应该中止,取消或停止所有pthread并记录运行的pthreads数量.

我的问题是我找不到如何列出正在运行的所有pthread.

c linux posix pthreads

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

如何检查静态库以查看是否正在导出调试符号?

我有一个静态库,我正在调试模式下构建,但当我进入它时,我仍然得到反汇编.我想知道如何使用nm或其他工具来确保调试符号没有被剥离.

c c++ static-libraries debug-symbols

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

((void*) - 1)是一个有效的地址吗?

来自Linux的man shmat逐字:

返回值

返回错误(void*)-1,并设置errno以指示错误原因.

(POSIX使用略有不同的措辞来讲述相同的内容.)

是否有任何强制性规则或定义(标准?)(void *) -1可能不是有效地址?

c void-pointers

17
推荐指数
3
解决办法
3419
查看次数

省略前瞻性声明(原型)

我在第14课就着名的" 学习C艰难之路 "在线课程.

在该课程中,它介绍了前向声明的概念C.代码示例中有两个前向声明.其中一个可以被注释掉,代码仍然编译,但另一个不能被注释掉.对我来说,他们看起来同样重要.

这是代码.它只是打印出所有字符及其十六进制代码(如果它们来自字母表),否则它会跳过它们.

两个编译器输出位于代码的底部.有人可以解释为什么一个错误出来而另一个错误?

#include <stdio.h>
#include <ctype.h>

// forward declarations
int can_print_it(char ch);       //NOT OK to skip(??)
void print_letters(char arg[]);  //OK to skip(??)

void print_arguments(int argc, char *argv[])
{
    int i = 0;

    for(i = 0; i < argc; i++) {
        print_letters(argv[i]);
    }
}

void print_letters(char arg[])
{
    int i = 0;

    for(i = 0; arg[i] != '\0'; i++) {
        char ch = arg[i];

        if(can_print_it(ch)) {
            printf("'%c' == %d ", ch, ch);
        }
    }

    printf("\n"); …
Run Code Online (Sandbox Code Playgroud)

c function

17
推荐指数
2
解决办法
687
查看次数

在C中使用printf打印空格数

我想知道我怎么能这样做,在CI中使用printf打印一定数量的空格正在考虑这样的事情,而且我的代码在第一个printf语句之后没有打印,我的程序编译完全很好.我猜我必须打印N-1个空格,但我不太清楚如何这样做.

谢谢.

#include <stdio.h>
#include <limits.h>
#include <math.h>

int f(int);

int main(void){
    int i, t, funval,tempL,tempH;
    int a;

    // Make sure to change low and high when testing your program
    int low=-3, high=11;
    for (t=low; t<=high;t++){
        printf("f(%2d)=%3d\n",t,f(t));                 
    }
    printf("\n");
    if(low <0){
        tempL = low;
        tempL *=-1;
        char nums[low+high+1];
        for(a=low; a <sizeof(nums)/sizeof(int);a+5){
            printf("%d",a);
        }
    }
    else{
        char nums[low+high];
        for(a=low; a <sizeof(nums)/sizeof(int);a+5){
            printf("%d",a);
        }
    }

    // Your code here...
    return 0;
}


int f(int t){
    // example 1
    return (t*t-4*t+5);

    // example …
Run Code Online (Sandbox Code Playgroud)

c printf

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

如何打印功能的地址?

gcc使用以下方法编译以下示例-Wall -pedantic:

#include <stdio.h>

int main(void)
{
  printf("main: %p\n", main); /* line 5 */
  printf("main: %p\n", (void*) main); /* line 6 */

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我明白了:

main.c:5: warning: format ‘%p’ expects type ‘void *’, but argument 2 has type ‘int (*)()’
main.c:6: warning: ISO C forbids conversion of function pointer to object pointer type
Run Code Online (Sandbox Code Playgroud)

第5行让我更改了第6行中的代码.

在打印功能的地址时删除警告我错过了什么?

c linux gcc function-pointers gcc-warning

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

Unix pthreads和信号:每个线程信号处理程序

我无法让线程捕获正确的信号.

例如,

我首先开始一个主线程(tid 1).

然后,它使用设置SIGUSR1函数1()的信号处理程序signal(2).

主线程使用tid 2创建一个新线程.

在螺纹2,I寄存器的信号处理程序SIGUSR1,以function2()使用signal(2).

然后,线程1创建一个线程3(tid 3).

从线程3,我pthread_kill(1, SIGUSR1)用来向线程1发送信号.

然而,function2()被召唤,而不是function1().

是出于这种行为,还是需要更改以使这些信号处理程序工作?

编辑:我已经做了一些调试,结果发现信号IS被发送到线程1,但是function2()由于某种原因从线程1调用.这有解决方法吗?

c multithreading signals pthreads

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