标签: pointers

为什么不在while(*s ++ =*t ++); 为我工作?

我在博客上看到了这个功能,我发现它非常酷.我理解它是如何在概念上运作的,因为C++是我的第一语言.

然而,当我尝试在我的程序中实际写出来时,它似乎不起作用.我已经用谷歌搜索过了,但我只是找到了关于它是如何工作的解释所以我真的很难过.

它不是复制NULL char [5](应该求值为false,不复制任何东西并打破循环),而是给出两个编译错误,说"我不能增加char [6] 类型的值"(out-of两个数组都出现错误.

为什么我的循环不会在char [5]中断?

我猜这是与char和string的微妙之处有关,我尝试初始化字符串而不是包含cstring,这也不起作用,"无法增加类型字符串"的类似错误显示出来.

#include <iostream>
using namespace std;

int main () {
     char s[] = "hello";
     char t[] = "house";
     while (*s++ = *t++);
     cout << s;
     return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ arrays string pointers strcpy

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

c4133警告c代码

我有一个问题,警告c4133,它说问题是从char*到int*的无法使用的类型,我尝试施放指针((char*)x)但没有运气,也许有人知道什么是问题/

这是我的程序,功能中的问题.

void replaceSubstring(char *str, char *substr)//function that gets string and substring and      make the substring in string big letters
{
    int i;
    int  *x;

    x = (strstr(str, substr));//The problem line 
    while (strstr(str,substr) != NULL)
    {
        for (i=0;i<strlen(substr);i++)
        {
            *x = *x - 32;
            x++;//move to the next char
        }
        x = (strstr(str, substr));  //first apear of substr int str
    }
 }
Run Code Online (Sandbox Code Playgroud)

c string pointers strstr

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

通过双指针强制分配内存

我不清楚当你通过双指针转换分配的内存时会发生什么,如:

char **ptr;
ptr=(char *)malloc(sizeof(char)*value); 
for(i=0;i<value;i++)
   ptr[i]=(char **)malloc(sizeof(char *)*another_value);
Run Code Online (Sandbox Code Playgroud)

在malloc的第一次调用期间,void*被转换为char*所以,我可以使用*(ptr + i)访问它,但是在malloc的第二次调用期间我不明白为什么我需要将void*转换为char**,不足以将它投射到char*?

c pointers casting

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

释放指针向量的两种不同方法 - 为什么不起作用?

我有一个指向结构的指针向量.解除分配内存的第一种方法工作正常,但第二种方法在执行后会产生分段错误(核心转储).为什么会这样?两个看起来都对我不对.用g ++编译.

struct milkNode {
    int unitCost;
    int unitsAvail;
};

int main() {

    //previous code

    for (int i = 0; i < M; ++i) {
        milkNode *cur = new milkNode;
        cur->unitCost = cost;
        cur->unitsAvail = avail;
        allNodes.push_back(cur);
    }


    //Method 1, works fine
    vector<milkNode*>::iterator iter;

    for (iter = allNodes.begin(); iter != allNodes.end(); ++iter) {
        delete *iter;
    }

    allNodes.clear();

    //Method 2, segmentation fault after execution. Compiles without errors.
    //Segmentation fault occurs just before the last "after" is printed

    /*
    vector<milkNode*>::iterator iter;
    for …
Run Code Online (Sandbox Code Playgroud)

c++ pointers memory-leaks memory-management

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

Int值奇怪地类似于指针

我已经发布了一些我在这里写的代码:http : //ideone.com/2TrjcN包含在给定的输出中.这是它的另一个副本:

#include <iostream>
using namespace std;

int Problem2(void);

int main() 
{
    int sum2 = Problem2();
    cout << "sum2 is: " << sum2 << endl;
}

int Problem2 (void)
{
    int sum = 0;
    int isEvenPrev = 0;
    int isEvenCurr = 2;

    for (int i=3 ; i<20; i++)
    {
            // determine if i is even
            if (i%2 == 0)
            {
                    isEvenPrev = isEvenCurr;
                    isEvenCurr = i;
                    sum += (isEvenPrev + isEvenCurr);
                    cout << "isEvenPrev: " << isEvenPrev …
Run Code Online (Sandbox Code Playgroud)

c++ pointers integer

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

将C字符串转换为全部更低

我正在尝试将C-String转换为所有小写,而不使用ctype.h中的tolower.我的代码似乎不起作用:我收到运行时错误.我正在尝试做的是更改大写字母bij'a' - 'A'的ASCII值,据我所知,它应该将这些值转换为小写的值.

#include <stdio.h>
void to_lower(char* k) {
    char * temp = k;
    while(*temp != 0) {
        if(*temp > 'A' && *temp < 'Z') {
            *temp += ('a' - 'A');
        }
        temp++;
    }
}

int main() {
    char * s = "ThiS Is AN eXaMpLe";
    to_lower(s);
    printf("%s",s);
}
Run Code Online (Sandbox Code Playgroud)

c string ascii pointers tolower

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

为什么代码在使用unique_ptr时崩溃,原始指针工作正常?

这是我的代码,我将值分配给树的节点.我可以很好地分配到树的左右孩子.但是当我尝试使用root的left-> left child时,它会给我访问冲突错误.

Trees.exe中0x00DE5CC3处的未处理异常:0xC0000005:访问冲突读取位置0x00000004.

正确地得到错误,unique_ptr<node> (r->left->left) = newNode(4);.

我正在使用unique_ptr,如果我使用原始指针,一切都按预期工作.

以下是我的代码,

using std::cout;
using std::endl;
using std::unique_ptr;


struct node
{
    int data;
    node * left;
    node * right;
};

unique_ptr<node> newNode (int i)
{
    unique_ptr<node> n (new node);
    n->data = i;
    n->left = nullptr;
    n->right = nullptr;

    return n;
}

int main(int argc, char* argv[])
{
    unique_ptr<node> r = newNode(1); 
    unique_ptr<node> (r->left) = newNode(2);
    unique_ptr<node> (r->right) = newNode(3);
    unique_ptr<node> (r->left->left) = newNode(4);//this line craches 
    unique_ptr<node> (r->left->right) = …
Run Code Online (Sandbox Code Playgroud)

c++ crash pointers unique-ptr c++11

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

带指针的C函数在一台计算机上工作,而在另一台计算机上不起作用

#include <stdio.h>
void swap (int *a, int *b)
 {
    int *tmp;
    *tmp = *a;
    *a = *b;
    *b = *tmp;
 }

int main ()
{
 int x = 5;
 int y = 7;
 swap (&x,&y);
 printf ("\n x = %d \n y = %d \n",x,y);
}
Run Code Online (Sandbox Code Playgroud)

我正在使用代码块,而且这段代码不起作用,我不明白为什么......在一台计算机上它运行得很好但在另一台计算机上它根本不会运行.有帮助吗?提前致谢.

c swap pointers function

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

通过指针阻止调用成员函数

考虑一下代码:

#include <iostream>

class Foo
{
public:
    Foo& operator*() = delete;
    Foo* operator->() = delete;
    void f()
    {
        std::cout << "Foo::f()" << std::endl;
    }
};

int main()
{
    Foo foo;
    // foo -> f(); // invokes deleted operator ->, so it doesn't compile

    Foo* pFoo = new Foo;    
    pFoo -> f(); // how can we make this not compilable?
    delete pFoo;
}
Run Code Online (Sandbox Code Playgroud)

有没有办法防止下一个最后一行pFoo -> f();编译?换句话说,我希望能够创建一个指向该对象的指针,但不能在调用成员函数时使用它.删除operator*()operator->()不起作用,因为它们仅由Foo指针调用,而不是由指针调用Foo.

PS:我希望能够在堆上创建对象,因此标记operator new为私有不是一种选择.

c++ pointers

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

使用memcpy后无法打印指针

我正在尝试使用自己的memcpy版本将字符数组从一个指针复制到另一个指针.但是,这会产生错误.看来printf存在问题.

其次,官方memcpy函数返回目标指针.这真的需要吗?如果我修改*dest,它应该反映在*d中.那么返回什么需要什么呢?

#include <stdio.h>

void memcpy2(void *dest, const void *src, size_t n)
{
    char *dp = dest;
    const char *sp = src;
    while (n--)
        *dp++ = *sp++;
}

int main(void) {
    char *c = "Hello";
    char *d=NULL;
    memcpy2(d,c,3);
    printf( "%c", *d);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c pointers

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