标签: pointers

没有指针的C++中的动态内存分配 - 指针的重点是什么?

考虑以下C++程序:

#include <iostream>
using namespace std;

void printArray(int p_values[], int size, int elements_set);

int main() 
{
    int next_element = 0;
    int size = 3;
    int p_values[size]; 
    int val;
    cout << "Please enter a number: ";
    cin >> val;

    while (val > 0)
    {
    if (size == next_element+1)
    {
        size *=2;
        int p_values[size];
    }
    p_values[next_element] = val;
    next_element++;
    cout << "Current array values are: " << endl;
    printArray(p_values, size, next_element);
    cout << "Please enter a number (or 0 to exit): "; …
Run Code Online (Sandbox Code Playgroud)

c++ memory arrays pointers dynamic

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

交换功能 - 指针 - 混乱

请注意代码中我没有使用指针,但我有一些概念,如果我使用这个函数,当代码块完成时,该值将恢复正常.

但代码正在编译,实际上我会用指针得到答案.

我需要帮助,因为如果我有与指针相关的犯规概念,我会感到困惑.

void swap(int i, int j) {
    int temp = i;
    i = j;
    j = temp;
}

int main() {
    int a = 110;
    int b = 786;
    cout << "Before swapping the value" << endl;
    cout << "'a' stores the value : " << a << endl;
    cout << "'b' stores the value : " << b << endl;
    swap(a,b);
    cout << "\nAfter swapping the value" << endl;
    cout << "'a' stores the value : " …
Run Code Online (Sandbox Code Playgroud)

c c++ pointers

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

通过指针访问结构“无法转换为指针类型”

gcc -g -O2    struct.c   -o struct
struct.c: In function ‘secondfunction’:
struct.c:19:2: error: cannot convert to a pointer type
  firstfunction((void *)onedata->c,(void *)&twodata.c,2);
  ^~~~~~~~~~~~~
<builtin>: recipe for target 'struct' failed
make: *** [struct] Error 1
Run Code Online (Sandbox Code Playgroud)

我试图通过memcpy将struct的内容复制到另一个struct时使用指针。但是,当我将指向该结构的指针转换为函数时,无法将其强制转换为空白。

struct one {
    char a;
    char b;
};

struct two {
    struct one c;
    struct one d;
};

void firstfunction(void *source, void *dest, int size)
{
    //memcpy(dest,source,size)
}

void secondfunction(struct two *onedata)
{
    struct two twodata;
    firstfunction((void *)onedata->c,(void *)&twodata.c,2);
}

void main()
{
    struct two onedata; …
Run Code Online (Sandbox Code Playgroud)

c struct pointers

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

无法在C中正确使用struct指针

我是C的初学者,目前正努力在函数中使用结构体.即使我给我的函数指向我的结构的指针并使用它来改变它们的值似乎我的函数无法改变指针本身的值

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

typedef struct Vector
{
    int n;
    double *entry;
}Vector; 

//defining the struct vector with n for its length and entry as a pointer which will become an array for its values
Vector *newVector(int n)
{
    int i = 0;
    Vector *X = NULL;
    assert(n > 0);
    X = malloc(sizeof(Vector));
    assert(X != NULL);
    X->n = n;
    X->entry = malloc(n+1*sizeof(double));
    assert(X->entry != NULL);
    X->entry[0] = 0;
    for (i=1; i<n+1; ++i) {
        scanf("%lf",&X->entry[i]);
    } …
Run Code Online (Sandbox Code Playgroud)

c arrays struct pointers

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

使用malloc时什么都没有指针?

据我所知,我已经遵循了其他用户提供的建议,但是当我尝试返回指向数组的指针时,我仍然会遇到段错误.main中的printf什么也没有返回,所以我猜测指针不是堆上的任何东西?

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

int * stringy(int length){
   int *ptr = malloc(sizeof(int)*length);
   int i;
   for(i = 0; 1 < length; i++){
      ptr[i] = i;
   }
   return(ptr);
}

void main(int argc, char **argv){
   int strlen = 12;
   int *newptr = stringy(strlen);
   printf("%d\n",newptr[0]);
   free(newptr);
}
Run Code Online (Sandbox Code Playgroud)

c arrays malloc pointers segmentation-fault

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

在C99中使用和不使用&打印指针之间的差异

请考虑以下代码:

    const char* text = "hi";
    printf("%s\n",text);
    printf("%p\n", &text);
    printf("%p\n", text);
Run Code Online (Sandbox Code Playgroud)

每个人从哪里printf获取它打印的价值?

有什么区别?

c pointers c99

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

c中函数指针的用例

我在阅读函数指针时遇到的一个常见用例是,它们可以用来使函数更加灵活,因为函数功能的一部分可以作为参数使用.这方面的一个例子是qsort,我可以在其中创建一个比较函数来决定更大和更小的含义(升序,降序,是等的倍数),并将比较函数指针传递给qsort函数.

这里,函数repeat具有addptr作为参数,因此执行乘法.

int add(int a, int b)
{
    return a+b;
}

int (*addptr)(int,int);

int repeat(int a,int b,int (*funcptr)(int,int))
{
    int i,ans=0;
    for(i=0;i<a;i++)
    {
        ans=(*funcptr)(ans,b);
    }
    return ans;
}


int main()
{
    addptr=&add;
    printf("%d\n",repeat(7,5,addptr));  

    return 0;

}
Run Code Online (Sandbox Code Playgroud)

但是在没有函数指针的情况下可以完成同样的事情!

int add(int a, int b)
    {
        return a+b;
    }


    int repeat(int a,int b,int func(int,int))
    {
        int i,ans=0;
        for(i=0;i<a;i++)
        {
            ans=func(ans,b);
        }
        return ans;
    }


    int main()
    {
        printf("%d\n",repeat(7,5,add)); 

        return 0;

    }
Run Code Online (Sandbox Code Playgroud)

那么为什么这甚至是函数指针的一个用途呢?第一个代码比第二个代码有什么优势?

c pointers function

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

使用c ++中的指针的数组:访问返回的数组时的分段错误

我是C++的新手,我正在尝试使用指向指针的指针来构建一个三维数组.我确信这样做有更有效的方法,但我正在努力理解指针.

作为示例代码,我最初有以下部分,它工作正常,分配,初始化和释放内存.

void builder(int aSize1, int aSize2, int aSize3)
{
    int i1, i2, i3;
    int ***frequencies;

    cout << "allocation started ..." << endl;
    frequencies = new int** [aSize1+1];
    for (i1=0; i1<=aSize1; i1++){
        frequencies[i1] = new int*[aSize2+1];
        for (i2 = 0; i2 <= aSize2; i2++)
        {
            frequencies[i1][i2] = new int [aSize3 + 1];
        }
    }
    cout << "allocation done" << endl;
    cout << " " << endl;

    cout << "before initialization" << endl;
    for (i1=0; i1<=aSize1; i1++){
        for(i2=0; i2<=aSize2; i2++){
            for(i3 …
Run Code Online (Sandbox Code Playgroud)

c++ arrays pointers segmentation-fault

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

在C中为字符串添加整数,如何理解结果?

如何输出结果是早上.

#include<stdio.h>
void main()
{
    printf(5+"Good Morning");
    return 0;
}

output is Morning
Run Code Online (Sandbox Code Playgroud)

c string pointers

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

为什么C中的空指针等于C中的另一个空指针?

比如考虑我声明两个指针NULL.然后,如果我比较它们,结果是真的.但是如果我们认为NULL是"没有"那么我们怎么能说两个"nothings"是相等的?是出于某种特定原因吗?任何帮助赞赏.:)

c null pointers

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

标签 统计

pointers ×10

c ×8

arrays ×4

c++ ×3

segmentation-fault ×2

struct ×2

c99 ×1

dynamic ×1

function ×1

malloc ×1

memory ×1

null ×1

string ×1