首页 \ 问答 \ 为什么我不能将NULL值赋给这个指针数组元素?(Why can I not assign NULL value to this pointer array element?)

为什么我不能将NULL值赋给这个指针数组元素?(Why can I not assign NULL value to this pointer array element?)

我仍然对C语言编程不熟悉。如果这种问题已经被问到,我很抱歉,我真的不知道该怎么搜索。

作为练习,我正在编写一本字典。 用户应该能够在练习的这个阶段添加,打印和删除单词。 单词存储在一个名为'dict'的字符指针数组中。 函数'removeWord'应该确保被删除的单词被替换为数组'dict'中的最后一个单词。 所以最后一个单词应该被复制到必须删除的元素上,最后一个元素应该被删除(即赋值为NULL)。 当要删除的单词是数组'dict'中的最后一个单词时,它也应该被删除。

当数组'dict'中的任何单词除去阵列中的最后一个单词时,该程序将停止运行。 当除了最后一个单词之外的任何单词被删除时,我想将NULL分配给最后一个单元。

我希望我的问题很清楚。 我非常感谢您提供的任何帮助。

int numberOfWordsInDict(char **dict)
{
    int i,cnt=0;
    for(i=0;i<10;i++)
    {
        if(dict[i]!=NULL)
        {
            cnt++;
        }
    }
    return cnt;
}

void addWord(char **dict, char *word)
{
    int i=0;
    if(numberOfWordsInDict(dict)==10)
    {
        printf("Dictionary is already full!\n");
    }
    int k=numberOfWordsInDict(dict);
    dict[k]=(char*)malloc((strlen(word)+1)*sizeof(char));

    strcpy(dict[k],word);
}


void printDict(char **dict)
{
    int i=0;
    printf("Dictionary:\n");
    if(numberOfWordsInDict(dict)==0)
    {
        printf("The dictionary is empty.\n");

    }else
    {
        for(i=0;i<10;i++)
        {
        printf("- %s\n", dict[i]);
        }
    }
}

void removeWord(char **dict, char *word)
{
    int i,j=0;
    int swapped=0;
    j=numberOfWordsInDict(dict);
    for(i=0;i<j;i++)
    {
        if(strcmp(dict[i],word)==0 && swapped==0)
        {
        swapped=1;
        //strcpy(dict[i],dict[j-1]);
        dict[j-1] = NULL;
        }
    }

}

在字典[j-1]发生错误。

int main()
{
    char wordToBeAdded[36]={};
    char wordToBeRemoved[36]={};
    char *dict[10]={};
    char operation;

    while(1)
    {
        printf("Command (a/p/r/q): ");
        scanf(" %c", &operation);

        switch(operation)
        {
            case 'a':
                printf("Add a word: ");
                scanf(" %s", &wordToBeAdded);
                addWord(dict,wordToBeAdded);
                break;

            case 'p':
                printDict(dict);
                break;

            case 'r':
                printf("Remove a word: ");
                scanf(" %s", &wordToBeRemoved);
                removeWord(dict,wordToBeRemoved);
                break;

            case 'q':
                return 0;
        }
    }
}

I am still new to programming in C. I am sorry if this kind of question has already been asked, I did not really know what to search for exactly.

As an exercise I am programming a dictionary. The user should be able to add, print, and remove words in this stage of the exercise. Words are stored in a character pointer array called 'dict'. The function 'removeWord' should make sure the word to be removed is replaced by the last word in the array 'dict'. So the last word should be copied to the element that has to be removed and subsequently the last element should be removed (i.e. assigned the value NULL to). When the word to be removed is the last word in the array 'dict' it should also be removed.

The program stops running when any word in the array 'dict' is removed except for the last one in the array. When any word except for the last one is removed I want to assign the value NULL to the last element.

I hope my question is clear. I really appreciate any help you can provide.

int numberOfWordsInDict(char **dict)
{
    int i,cnt=0;
    for(i=0;i<10;i++)
    {
        if(dict[i]!=NULL)
        {
            cnt++;
        }
    }
    return cnt;
}

void addWord(char **dict, char *word)
{
    int i=0;
    if(numberOfWordsInDict(dict)==10)
    {
        printf("Dictionary is already full!\n");
    }
    int k=numberOfWordsInDict(dict);
    dict[k]=(char*)malloc((strlen(word)+1)*sizeof(char));

    strcpy(dict[k],word);
}


void printDict(char **dict)
{
    int i=0;
    printf("Dictionary:\n");
    if(numberOfWordsInDict(dict)==0)
    {
        printf("The dictionary is empty.\n");

    }else
    {
        for(i=0;i<10;i++)
        {
        printf("- %s\n", dict[i]);
        }
    }
}

void removeWord(char **dict, char *word)
{
    int i,j=0;
    int swapped=0;
    j=numberOfWordsInDict(dict);
    for(i=0;i<j;i++)
    {
        if(strcmp(dict[i],word)==0 && swapped==0)
        {
        swapped=1;
        //strcpy(dict[i],dict[j-1]);
        dict[j-1] = NULL;
        }
    }

}

at dict[j-1] the error occurs.

int main()
{
    char wordToBeAdded[36]={};
    char wordToBeRemoved[36]={};
    char *dict[10]={};
    char operation;

    while(1)
    {
        printf("Command (a/p/r/q): ");
        scanf(" %c", &operation);

        switch(operation)
        {
            case 'a':
                printf("Add a word: ");
                scanf(" %s", &wordToBeAdded);
                addWord(dict,wordToBeAdded);
                break;

            case 'p':
                printDict(dict);
                break;

            case 'r':
                printf("Remove a word: ");
                scanf(" %s", &wordToBeRemoved);
                removeWord(dict,wordToBeRemoved);
                break;

            case 'q':
                return 0;
        }
    }
}

原文:https://stackoverflow.com/questions/45618585
更新时间:2023-06-18 22:06

最满意答案

让我们按照步骤输出:

git checkout svnMirror

Switched to branch 'svnMirror' - 好

git svn rebase

你有Current branch svnMirror is up to date. - 很好

git push github svnMirror

你有Everything up-to-date - 好。 所以你没有什么可推动的。 然后继续...

git checkout master

你被Switched to branch 'master'.Your branch is ahead of 'github/master' by 7 commits.

git merge svnMirror

Already up-to-date. 没有什么可以从svnMirror分支进行合并。

它已经完成了你想要它做的一切。 请注意,这是主人提前github /主7提交,你不推它。


Let us follow the steps and output:

git checkout svnMirror

you got Switched to branch 'svnMirror' - Good

git svn rebase

you got Current branch svnMirror is up to date. - Good

git push github svnMirror

you got Everything up-to-date - Good. So you had nothing to push. Then it continues...

git checkout master

you got Switched to branch 'master'.Your branch is ahead of 'github/master' by 7 commits.

git merge svnMirror

you got Already up-to-date. There is nothing to merge from svnMirror branch.

It has done all that you wanted it to do. Note, it is master that is ahead of github/master by 7 commits and you are not pushing it.

相关问答

更多
  • 根据git help svn : rebase This fetches revisions from the SVN parent of the current HEAD and rebases the current (uncommitted to SVN) work against it. ...我认为git svn rebase仅适用于当前分支,所以你所在的分支确实很重要。 According to git help svn: rebase ...
  • 让我们按照步骤输出: git checkout svnMirror 你Switched to branch 'svnMirror' - 好 git svn rebase 你有Current branch svnMirror is up to date. - 很好 git push github svnMirror 你有Everything up-to-date - 好。 所以你没有什么可推动的。 然后继续... git checkout master 你被Switched to branch 'ma ...
  • 尝试“git svn rebase”。 我遇到了完全相同的问题,并使用托管@vhttps://github.com/nirvdrum/svn2git的包装脚本。 请花时间阅读SVN,以使用svn2git进行GIT迁移失败 。 Try "git svn rebase". I faced exactly the same issue and used a wrapper script hosted @vhttps://github.com/nirvdrum/svn2git. Please take come t ...
  • GitHub上的svn接口可能对你有用 - 你可能想先检查一下。 它会自动保持最新的主分支。 svn co http://svn.github.com/user/project 但是,如果这还不够,可以在推送内容时为GitHub提供ping的ping,然后设置服务器以运行git pull; svn ci git pull; svn ci只要它得到ping。 It's possible that the svn interface on GitHub may work for you - you might ...
  • 发布SVN分支是安全的,但git-svn dcommit是所有提交都使用git-svn dcommit推送到SVN git-svn dcommit 。 如果你在分支上没有任何变化,那么git-svn rebase只会快速前进。 如果有人从您发布的分支中分支出来,重要的是他们知道它来自SVN回购。 这是因为如果你试图接受他们的改变,推动他们进入SVN回购的唯一方法就是重新调整他们的改变。 在您再次发布提交的更改后,他们将不得不处理冲突的提交,因为哈希将不匹配。 在master工作是安全的,但可能不实际。 从上 ...
  • 您已经描述了SubGit的工作原理。 从2.0开始,它允许使用预接收Git钩子(在'git push'上执行)在Git和SVN之间进行双向转换。 要在你的机器上运行 $ subgit configure --svn-url repo.git $ #adjust repo.git/subgit/{config,passwd,authors.txt} to set auth options, branches to translate and SVN<->Git ...
  • 您可以使用git reflog查找在rebase之前描述树状态的哈希,然后将其检出。 You can use git reflog to find the hash that described the state of your tree before the rebase and then check it out.
  • 您不能使用与基于SVN的分支合并(阅读git svn手册;它在多个地方非常仔细地说明这一点)。 如果需要合并,请将SVN树合并到其他分支中的其他方式(但绝不会将其他内容合并到SVN树中)。 让git svn拥有master分支,如果你需要在一个单独的分支中进行开发,请自己使用另一个(比如master2 )。 然后,当您希望将其中的工作提交到主分支时,请执行以下操作: git checkout master2 git rebase master git checkout master git merge -- ...
  • 我的调查结果:git-svn的rebase函数没有什么神奇之处。 它只是一个git svn fetch然后是git rebase refs/remotes/trunk和refs update。 在我的情况下,我所拥有的只是将我的本地跟踪分支ref移动到从提交中获取的最后一个。 git svn fetch git log -1 refs/remotes/trunk 给了我最新的sha1:ed0fa874ca872bc3a0101ee397f611a537e72c2a git update-ref HEAD ...
  • 如果您准备完全忽略git端发生的任何事情,您可以执行以下顺序: 从svn获取最新信息 git svn fetch 假设您想将svn/trunk推送到origin/master -f强制svn / trunk的当前状态覆盖远程端的任何内容 git push -f origin svn/trunk:master In case you are ready to completely ignore whatever happens on the git side, you could do the follo ...

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)