标签: pointers

C#字节中这个奇怪的语法是什么*

可能重复:
DotNet - 什么是int*?

嗨,我正在查看一个库的一些源代码,我看到了这个byte*,最后的明星是什么?,不仅有类字节,还有一些变量名称之类的

var1 = *var3 - *var2;
Run Code Online (Sandbox Code Playgroud)

提前致谢

.net c# pointers

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

使用C覆盖数组中的字符

我在C中创建一个动态的二维字符数组:

注意:rows并且columns是用户输入integers

char** items;
items = (char**)malloc(rows * sizeof(char*));
int i;
for(i = 0; i < rows; i++)
{
    items[i] = (char*)malloc(columns * sizeof(char));
}

int j;
for(i = 0; i < rows; i++)
{
    for(j = 0; j < columns; j++)
    {
        items[i][j] = 'O';
    }
}
Run Code Online (Sandbox Code Playgroud)

稍后在我的代码中,我尝试覆盖数组中的特定位置:

items[arbitraryRow][arbitraryColumn] = 'S';
Run Code Online (Sandbox Code Playgroud)

但结果是该行/列中的字符现在是'SO'

我究竟做错了什么?

更新:这是我打印数组的方式:

int i;
for(i = 0; i < rows; i++)
{
    printf("[");
    int j;
    for(j = 0; j < columns; …
Run Code Online (Sandbox Code Playgroud)

c arrays gcc pointers

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

理解C:指针和结构

我正在努力更好地理解c,而我很难理解我在哪里使用*和&字符.而且只是结构一般.这是一些代码:

void word_not(lc3_word_t *R, lc3_word_t A) {
    int *ptr;
    *ptr = &R;
    &ptr[0] = 1;
    printf("this is R at spot 0: %d", ptr[0]);
}  
Run Code Online (Sandbox Code Playgroud)

lc3_word_t是一个像这样定义的结构:

struct lc3_word_t__ {
  BIT b15;
  BIT b14;
  BIT b13;
  BIT b12;
  BIT b11;
  BIT b10;
  BIT b9;
  BIT b8;
  BIT b7;
  BIT b6;
  BIT b5;
  BIT b4;
  BIT b3;
  BIT b2;
  BIT b1;
  BIT b0;
};
Run Code Online (Sandbox Code Playgroud)

这段代码没有做任何事情,它编译但是一旦我运行它我得到一个"分段错误"错误.我只是想了解如何读取和写入结构并使用指针.谢谢 :)


新守则:

void word_not(lc3_word_t *R, lc3_word_t A) {
    int* ptr;
    ptr = &R;
    ptr->b0 = 1;
    printf("this is: …
Run Code Online (Sandbox Code Playgroud)

c pointers data-structures

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

创建一个int数组,其大小基于NSArray的大小

我正在尝试int根据运行时获得的大小创建并归零一个s 数组:

size = [gamePiece.availableMoves.moves count]; //debugger shows size = 1;
int array[size]; //debugger shows this as int[0] !
memset(array, 0, size);
indexes = array;
Run Code Online (Sandbox Code Playgroud)

size并且indexes都是这个类的ivars:

int size;
int* indexes;
Run Code Online (Sandbox Code Playgroud)

不过,我最终得到了一个0长度的数组.如何用指示的尺寸创建它[gamePiece.availableMoves.moves count]

c pointers objective-c dynamic-arrays

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

在c中修改char指针的值会产生段错误

以下代码在我的系统上产生分段错误.我无法弄清楚为什么.任何帮助,将不胜感激.

#include<stdio.h>
int main() {
    char * a = "abc";
    *a = 'c';
    printf("%c\n", *a);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c pointers char dereference

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

C中的Malloc问题

解决方案:在程序退出后释放已分配的内存.必须从磁盘读取+写回链表,然后重写以更新数据库!谢谢大家=)

您好,我基本上已经在这个数据库程序中工作了几天,但我只是不断达到死胡同.这项任务今天到期,所以如果你能帮助我,我将非常感激.= T

数据库使用链接列表实现,由几个文件组成:sdbm.c,sdbm.h,new.c,get.c,insert.c,put.c和remove.c.sdbm.c包含基于sdbm.h接口的数据库方法,其他文件包含使用sdbm方法的主要方法.

第一个问题来自insert程序,当我尝试添加一个键和值对时似乎工作正常......也就是说,直到我们再次尝试调用insert程序.分配的内存似乎已经消失了!我一直在研究,试图弄清楚为什么即使我有malloced,为什么它会在插入程序退出后消失.这是一些代码:

  • 节点结构+全局变量:
struct dbase_Node {
  char *keyValue;
  char *element;
  struct dbase_Node *next;
};

typedef struct dbase_Node Node;

Node *head;
Run Code Online (Sandbox Code Playgroud)

========

  • 插入方法
static bool sdbm_insert_back(Node **headRef, const char *key, const char *value)
{
  Node *new = (Node *)malloc(sizeof(Node));
  if (new == NULL)
    return false;
  else {
    new->keyValue = malloc(strlen(key));
    new->element = malloc(strlen(value));
    strcpy(new->keyValue, key);
    strcpy(new->element, value);

    new->next = *headRef;
    *headRef = new;
    return true;
  }
}
Run Code Online (Sandbox Code Playgroud)
  • 同步方法
bool sdbm_sync()
{
  if …
Run Code Online (Sandbox Code Playgroud)

c database memory malloc pointers

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

需要帮助理解c中的指针

我无法理解指针概念,下面是代码.为什么交换(&a1,&a2)输出-5,6而不是6,-5?这些值已经交换了吗?

void swap(int *ptr1, int *ptr2){

     int temp;

      temp = *ptr1;
      *ptr1 = *ptr2;
      *ptr2 = temp;

}

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

    void swap(int *ptr1, int *ptr2);
    int a1 = -5;
    int a2 = 6;
    int *p1 = &a1;
    int *p2 = &a2;

    NSLog(@" a1 = %i, a2 =%i", a1, a2); // out puts: -5, 6

    swap(p1,p2);
    NSLog(@" a1 = %i, a2 =%i", a1, a2); // out puts: 6, -5

    swap(&a1, &a2);
    NSLog(@" a1 = %i, a2 =%i", …
Run Code Online (Sandbox Code Playgroud)

c pointers objective-c concept

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

在C++中动态分配列表

我制作了一个可爱的通用(即模板)List类来处理C++中的列表.原因是我发现这个std::list类在日常使用中非常难看,而且由于我经常使用列表,我需要一个新的.主要的改进是,我的班级,我可以用来[]从中获取物品.此外,还有待实现的是一个IComparer对事物进行排序的系统.

我正在使用这个ListOBJLoader,我的类加载Wavefront .obj文件并将它们转换为网格.OBJLoader包含指向以下"类型"的指针列表:3D位置,3D法线,uv纹理坐标,顶点,面和网格.顶点列表具有必须链接到所有3D位置,3D法线和uv纹理坐标列表中的某些对象的对象.面链接到顶点,网格链接到面.所以他们都是相互联系的.

为简单起见,让我们考虑一下,在某些情况下,只有两个指针列表:List<Person*>List<Place*>.Personclass包含,其中包括字段List<Place*> placesVisitedPlace类包含字段List<Person*> peopleThatVisited.所以我们有结构:

class Person
{
    ...
  public:
    Place* placeVisited;
    ...
};

class Place
{
    ...
  public:
    List<People*> peopleThatVisited;
};
Run Code Online (Sandbox Code Playgroud)

现在我们有以下代码:

Person* psn1 = new Person();
Person* psn2 = new Person();

Place* plc1 = new Place();
Place* plc2 = new Place();
Place* plc2 = new Place();


// make some links between …
Run Code Online (Sandbox Code Playgroud)

c++ pointers list dynamic-memory-allocation dangling-pointer

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

在C中,我可以将字符串保存为char*,然后使用指针指向其他字符串吗?

如果我创建一个类似的字符串

char string[6] = "hello";
char* ptr = (char*) malloc(sizeof(string));
strcpy(ptr, string);
Run Code Online (Sandbox Code Playgroud)

然后我这样做:

char* ptr2 = ptr;
Run Code Online (Sandbox Code Playgroud)

它会起作用吗?

一般来说,如果我有一个指向字符串的char*指针,我可以让其他指针指向该字符串并且初始指针指向另一个字符串吗?

[编辑]

感谢您的反馈,但我有点困惑.我会尝试从头开始.

  1. 我有我的主要功能,我在其中执行scanf来读取一个字符串,该字符串不是固定大小但具有MAX长度.我这样看了char* input; scanf("%as", &input);.我认为'a'标志会自动分配所需的内存以适应字符串.

  2. 然后我必须调用一个foo(char* s)以字符串作为输入的函数.

  3. 在该函数内部,我必须将字符串存储在内存中.我有两个选择:使用参数或创建一个新的malloc和strcpy并使用新的字符串.哪两个是正确的?我只需要存储字符串的内存位置,稍后再参考.

  4. 如果我使用参数并在某处存储它的内存位置,这是安全的还是因为指向字符串的指针是常量会导致任何问题吗?用malloc制作另一个字符串会解决任何问题,还是同样的事情?

直到现在我已经尝试了几种组合,但我无法达到我想要的效果.任何帮助表示赞赏.

c pointers char

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

结构成员语法

我无法访问结构的成员

代码如下:

int main()
{

      typedef struct tempA
       {
         int a;
        }tempa;


      typedef struct tempB
      {
        tempa **tA;
       }tempb;

     tempb.(*tA)->a =5;
     printf("\n Value of a : %d",tempb.(*tA)->a);

}
Run Code Online (Sandbox Code Playgroud)

我试图使用它来访问它, tempb.(*tA)->a;但我收到语法错误:

error: expected identifier before ‘(’ token 
Run Code Online (Sandbox Code Playgroud)

访问的正确语法是int a什么?

提前致谢

c syntax struct pointers

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