标签: pointers

在函数返回中将数组转换为指针

我在C++中遇到了关于指针的问题.代码是:

int * initArray() {
    int a [2];

    a[0] = 1;
    a[1] = 2;
    return a;
}


int main () {
    int * b = initArray();

    cout << b << "---" << *(b) << endl;
    cout << b + 1<< "---" << *(b + 1) << endl;
}
Run Code Online (Sandbox Code Playgroud)

输出是

0021FC20---1
0021FC24---1431629120
Run Code Online (Sandbox Code Playgroud)

你可以看到值是错误的.

当我尝试将init数组代码放入main函数时,它正确运行.

你能告诉我的代码有什么问题吗?

c++ pointers return function

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

c ++中的指针和数组

我理解C中指针和数组的概念.我的问题是它们的语法.这是什么:

char* var;
char var*;
Run Code Online (Sandbox Code Playgroud)

c++ arrays pointers

0
推荐指数
2
解决办法
278
查看次数

指针包含什么类型的数据

我写了以下代码

int num =   3;
int *ptr = &num;

cout << *ptr << " " << ptr << endl;
Run Code Online (Sandbox Code Playgroud)

并得到以下输出.

3 0x7fff5fbff43c
Run Code Online (Sandbox Code Playgroud)

我想知道这是什么类型的数据.

0x7fff5fbff43c

是吗

一个.签名浮点数据

湾 有符号整数数据

C.无符号浮点数据

d.无符号整数数据

即 签名字符数据

c++ memory pointers memory-address

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

如何检查对象是否在数组中?

我知道如果数组中甚至没有对象,那么仍然有一些地址.但我想找到解决方案来检查我们会询问的特定索引下是否有对象.我需要有这样的机制来向多边形添加新的点.但在此之前,我需要知道如果有一个对象,对象的计数器是否应该增长或保持相同的值.也许我应该尝试填充所有数组NULL

main.cpp中

#include <iostream>
#include "punkt.h"
#include "wielokat.h"

using namespace std;

int main(int argc, char *argv[])
{
    Punkt  p1("p1", 10, 20); // 10x20
    Punkt  p2("p2", 1, 1);   //1x1

    Wielokat w1 ("square", 4);

    w1.set(p1,0);
    w1.set(p2,0);
    w1.showWielokat();


    system("PAUSE");
    return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

Wielokat.cpp

#include "punkt.h"
#include "wielokat.h"
#include <iostream>

using namespace std;

void Wielokat::increase(int n)
{
    m_ilosc = m_ilosc + n;
    m_tab = new Punkt * [m_ilosc];  
    cout<<"Dodaj "<<m_ilosc<<endl;
}

void Wielokat::decrease(int n)
{
    m_ilosc = m_ilosc - n;
    if(m_ilosc<0){ m_ilosc=0;} …
Run Code Online (Sandbox Code Playgroud)

c++ arrays pointers function

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

在C中实现哈希表

我在用C实现一个简单列表时遇到麻烦,问题是通过指针连接项目。
下面的代码是哈希表的一部分,该哈希表应将具有相同索引的项目存储在列表中,以避免发生冲突。

typedef struct dictEntry {
    void *key;
    void *value;
    struct dictEntry *next;
} dictEntry;

typedef struct dict {
    dictEntry **table;
    unsigned long size;
    unsigned long used;
} dict;

void dictAdd(dict *d, void *key, void *value) {
    int index = hash(key) & d->size;
    dictEntry *entry;

    entry = malloc(sizeof(entry));

    entry->key   = key;
    entry->value = value;
    entry->next  = 0;

    if (d->table[index]) {
        /* this is does not work */
        dictEntry *next;
        next = d->table[index];

        while (next) {
            next = next->next;
        }

        next …
Run Code Online (Sandbox Code Playgroud)

c pointers hashtable linked-list

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

使用自指针将值分配给类数据成员时的差异

假设我有一个类:

Class MyClass 
{ 
  int myVar1;
  int myVar2;

  void myMethod(); 
}
Run Code Online (Sandbox Code Playgroud)

下面的两个价值分配有什么不同吗?

void MyClass::myMethod()
{
   //VARIABLE ASSIGNMENT
   myVar1 = 99;

   //USING POINTER TO CLASS
   this->myVar2 = 99;
}
Run Code Online (Sandbox Code Playgroud)

有什么用?

这 - >

变量赋值中的指针(例如,通过函数等传递类的其他情况除外)?

c++ oop pointers class

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

将const int转换为指针?

可能重复:
通过非const指针修改const

我有以下代码:

const int x = 5;
int *p = (int*)&x;

*p = 2; // Line 1

cout << x << " - " << *p << endl;
cout << &x << " - " << p << endl;
Run Code Online (Sandbox Code Playgroud)

并得到了结果:

5 - 2
0012FF28 - 0012FF28
Run Code Online (Sandbox Code Playgroud)

我知道代码很奇怪,不应该这样做.但我想知道为什么相同的地址但得到不同的结果?哪个Line 1商店2号?

c++ pointers casting

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

访问指向向量c ++的指针

我无法访问以下向量.我是向量的新手,所以这可能是一个很小的语法,我做错了.这是代码....

void spellCheck(vector<string> * fileRead)
{   
    string fileName = "/usr/dict/words";
    vector<string> dict;        // Stores file

    // Open the words text file
    cout << "Opening: "<< fileName << " for read" << endl;

    ifstream fin;
    fin.open(fileName.c_str());

    if(!fin.good())
    {
        cerr << "Error: File could not be opened" << endl;
        exit(1);
    }
    // Reads all words into a vector
    while(!fin.eof())
    {
        string temp;
        fin >> temp;
        dict.push_back(temp);
    }

    cout << "Making comparisons…" << endl;
    // Go through each word in vector
    for(int i=0; …
Run Code Online (Sandbox Code Playgroud)

c++ pointers function vector

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

指向struct中struct的结构

我在c中的结构有问题.

我有两个结构

typedef struct
{
    char isim[256];
    int deger;
    struct ekstra *sonra;
}ekstra;

typedef struct
{
    char *name;
    int val;
    struct ekstra *next;
}node;

/*and main is*/

int main()
{
    int i;
    node dizi[12];

    for(i=0;i<12;i++)
    {
        dizi[i].name = malloc("asdasd"*sizeof(int));
        strcpy (dizi[i].name,"asdasd");
        /*and trouble starts here*/
        **dizi[i].next = malloc(sizeof(ekstra));
        printf("%s",dizi[i].next->isim);**
    } 
}
Run Code Online (Sandbox Code Playgroud)

错误是

错误:取消引用指向不完整类型的指针

我怎么能举办dizi[i].next

c struct pointers dynamic

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

这种情况如何?

我有以下函数,它是Trie结构实现的一部分:

int alpha_char_strlen (const AlphaChar *str) {
    const AlphaChar *p;
    for (p = str; *p; p++) ;
    return p - str;
Run Code Online (Sandbox Code Playgroud)

}

任何人都可以帮助我解释for循环的条件是如何保持的,在这种情况下究竟是什么条件?
注意:AlphaChar只是一个带unsigned int类型的typedef,该函数计算AlphaChar字符.

c++ pointers

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