将偏移表示法转换为指针算术 [英] Converting offset notation to pointer arithmetic

查看:85
本文介绍了将偏移表示法转换为指针算术的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图用2d指针数组完成一个赋值。当我意识到这是我应该使用指针算法的要求之一时,我正在经历这个过程,但我一直在使用偏移表示法。所以我的问题是你们是什么是将我的偏移表示法转换为指针运算而不完全重写程序的最佳方法???另外,当遍历我的2d数组时,我为outofbounds函数调用了哪些参数才能正常工作?任何建议将不胜感激,并提前感谢您。



So I am attempting to complete an assignment using 2d pointer arrays. I was going through the process when I realized that was one of the requirements was that I was supposed to use pointer arithmetic, but instead I have been using offset notation. So my question for you guys is what is the best method of converting my offset notation into pointer arithmetic without completely rewriting the program??? Also when transversing through my 2d array what parameters do I call for my outofbounds function in order for it to properly work? Any suggestions would be greatly appreciated and thank you in advance.

//move through string by parsing  to insert each char into array element position

void rules(char** boardArr,int  &rows, fstream inFile, string &line, int &cols)
{
    char* pos;
    char ncount;
    for(int i = 0; i < rows; i++) //rows
    {
        getline(inFile, line);

        for(int j = 0; j < cols; j++) //cols
        {
            *(*(boardArr + i )+ j) == pos;//parsing string into bArr

            //neighbor check nested for organism
            pos  = *(*(boardArr + i)+ j);//position of index within
            if(*(*(boardArr + i+1)+ j)=='*')//checking pos to the right of pos index
            {
                //outofbounds()
                ncount++;
            }
            if(*(*(boardArr + i-1)+ j)=='*')//checking pos to the left of pos index
            {
                //outofbounds()
                ncount++;
            }
            if(*(*(boardArr + i)+ j+1)=='*')//checking pos to the above of pos index
            {
                //outofbounds()
                ncount++;
            }
            if(*(*(boardArr + i+1)+ j+1)=='*')//checking pos to the above and to the right of pos index
            {
                //outofbounds()
                ncount++;
            }
            if(*(*(boardArr + i-1)+ j+1)=='*')//checking pos above and to the  left of pos index
            {
                //outofbounds()
                ncount++;
            }
            if(*(*(boardArr + i-1)+ j-1)=='*')//checking pos below and to the left of pos index
            {
                //outofbounds()
                ncount++;
            }
            if(*(*(boardArr + i-1)+ j)=='*')//checking pos below of pos index
            {
                //outofbounds()
                ncount++;
            }
            if(*(*(boardArr + i-1)+ j+1)=='*')//checking pos below and to the right of pos index
            {
                //outofbounds()
                ncount++;
            }
            //row[i, row[i]-1])
            //cout<<*(*(boardArr + i)+ j);//assigning position to check for neighbors

        }



    }

//how to move through 2d array pointer arithmetic style

//boardArr[rows][cols] == *(*(boardArr + rows)+ cols)

//keep relationship between the numbers
//*(())
//If a cell contains an organism and has fewer than 2 neighbors, the organism dies of loneliness.
//A neighbor is an organism in one of the 8 spots (or fewer if on the edge) around a cell
//If a cell contains an organism and has more than 3 neighbors, it dies from overcrowding.
// If an empty location has exactly three neighbors, an organism is born in that location.
//returns nothing
}
bool  outofbounds( int &rows, int &cols, int i, int j)
{
    if((i >0 && i< rows)  && (j < cols && j > 0))
    {
        return true;
    }
    else
        return false;
}





我的尝试:



用Google搜索正确的指针文档

重新开始考虑。



What I have tried:

Googling proper documentation of pointers
Contemplated starting over.

推荐答案

*(*(boardArr + i )+ j) == pos;//parsing string into bArr

//neighbor check nested for organism
pos  = *(*(boardArr + i)+ j);//position of index within



用任何语言处理内存的最糟糕的方法之一;只是不要这样做。并且第一个语句什么都不做,但是如果你打算写一个 = 符号,第一次 pos 没有这样会导致问题。



首先设置指向数组各部分的指针,例如


One of the worst ways to address memory in any language; just don't do it. And the first statement does nothing, but if you meant to write a single = sign, first time round pos has no value so that will cause problems.

Start by setting pointers to each part of the array e.g.

pos = *boardArr;  // point to the first element of boaradArr



我不确定其余代码应该做什么,所以很难添加更多。可以说你可以创建其他指针作为pos的正偏移或负偏移。


I am not sure what the rest of the code is supposed to be doing so it is difficult to add more. Suffice to say you can create other pointers as positive or negative offsets from pos.


这段代码可以完成这项工作。

This code should do the job.
char** pointer = boardArr;//set initial running 
for(int i = 0; i < rows; i++) //rows
{
    getline(inFile, line);

    for(int j = 0; j < cols; j++) //cols
    {
        *pointer = pos;//parsing string into bArr
        pointer++;//move pointer one memory block size
    }
    pointer++;//move pointer one memory block size

请测试++运算符是否执行了正确的工作。



备注:当您使用一列时,下一个偏移在列后开始。所以

Please test that the ++ operator does the correct job.

Remark: when you work one column, the next offset starts after the column. So

int offset = (i * row) + j;//imagine a chess board

PS:我的经验是说仔细检查是指针算术的基础。这是一个常见的问题来源:-O

PS: my experiences saying that careful bounds checking is the fundament of pointer arithmetics. It is a common source of problems :-O


这篇关于将偏移表示法转换为指针算术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆