小编MC9*_*C93的帖子

在不同的体系结构上转换指针类型

我有以下结构和"getter"函数,它返回a强制转换为无符号整数:

struct s {
    uint32_t a;
};

void get_a(struct s *st, unsigned *ret)
{
    *ret = (unsigned)st->a;
}
Run Code Online (Sandbox Code Playgroud)

运行以下代码:

struct s st;
uint16_t x;

st.a = 1;
get_a(&st, (unsigned *)&x);
Run Code Online (Sandbox Code Playgroud)

对于x86_64,i686,armv7hl,ppc64le等架构x == 1,但对于ppc64 x == 0.为什么是这样?Little-vs. big-endian?

c pointers casting endianness

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

动态分配矩阵的功能

我想创建一个函数来分配(带malloc/ calloc)声明为双指针的矩阵.我理解双指针矩阵如何工作以及如何分配它malloc,但当我传递我的矩阵(声明main()并初始化NULL)时,我的程序崩溃了.我想错误与我的allocMatrix()功能有关,因为如果我在main所有工作中顺利分配矩阵.谢谢 :-)

主要:

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

#define ROW 5
#define COL 5

int main(void) {

    int i,j, ret;
    int nRow, nCol;
    int **mat=NULL; // double pointer matrix

    nRow = 0;
    nCol = 0;

    //Insert n of row and columns
    printf("Insert n of rows and columns:\n");
    scanf("%d %d", &nRow, &nCol);

    //Functions to allocate matrix
    ret=allocMatrix(mat, nRow, nCol);
    printf("Return value: %d\n",ret);

    /*
    this code works …
Run Code Online (Sandbox Code Playgroud)

c malloc pointers dynamic matrix

3
推荐指数
2
解决办法
5363
查看次数

atoi()函数如何处理非数字情况?

我试图编写自己的atoi()函数实现,并尝试了两个不同的代码:

#include <stdio.h>
#include <string.h>

int myatoi(const char *string);

int main(int argc, char* argv[])
{
    printf("\n%d\n", myatoi("str"));
    getch();

    return(0);
}  

int myatoi(const char *string){
    int i;
    i=0;
    while(*string)
    {
        i=(i<<3) + (i<<1) + (*string - '0');
        string++;
        // Don't increment i!
    }
    return i;
}
Run Code Online (Sandbox Code Playgroud)

和

#include <stdio.h>
#include <string.h>

int main() {
    char str[100];
    int x;
    gets(str);
    printf("%d",myatoi(str));
}

int myatoi(char *str) {

    int res =0;
    int i;

    for (i = 0; str[i]!= '\0';i++) {
        res = …
Run Code Online (Sandbox Code Playgroud)

c atoi

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

大熊猫数据框中点坐标的欧式距离

我有一个六列的pandas数据框,前三列包含x,y和z参考坐标,接下来的三列是某个点的坐标。我想将这两点之间的欧几里得距离放在数据框的新列中。我考虑使用numpy.linalg.normvia pandas.apply方法,但是我不知道解析numpy函数的数据帧行的最佳方法是什么。你能给我一些建议吗?

python numpy dataframe pandas

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

预测C代码中的缺陷

最近我参加了一次采访,有几个问题,我必须找到代码中的缺陷.以下是问题.

void fun(char *p)   
{    
    int a = 0;

    int b = strlen(p) - 1;

    int d = 0;

    while(d == 0)
    {
        if(a == b)
        {
            d = 1;
        }
        else
        {
            char t = *(p + a);
            *(p + a) = *(p + b);
            *(p + b) = t;

            a += 1;
            b -= 1;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的回答是:

  1. 没NULL检查p.
  2. 错误的条件if(a == b).如果字符串的长度是偶数,则这种情况永远不会满足.

如果有人发现任何其他缺陷,请告诉我,并且欢迎任何对我给出的答案的评论.

c

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

从.txt文件中获取信息

我有一个.txt包含列表的文件(这是列表的一个例子):

[[[[1155, 87]], [[1155, 612]], [[385, -81]], 0, 0, 0, 0, 0, 0, 0], [151, 130, 83, 0, 0, 0, 0, 0, 0, 0], [2, 4, 2, 0, 0, 0, 0, 0, 0, 0], [22, 31, 1, 0, 0, 0, 0, 0, 0, 0], [(1, 2, 3), (1, 2, 3), (1, 2, 3), 0, 0, 0, 0, 0, 0, 0], [[2, 3], [1, 3], [3], 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, …
Run Code Online (Sandbox Code Playgroud)

python string list character

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

标签 统计

c ×4

pointers ×2

python ×2

atoi ×1

casting ×1

character ×1

dataframe ×1

dynamic ×1

endianness ×1

list ×1

malloc ×1

matrix ×1

numpy ×1

pandas ×1

string ×1