为什么我不能将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
最满意答案
让我们按照步骤输出:
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'
- Goodgit svn rebase
you got
Current branch svnMirror is up to date.
- Goodgit 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 ...
-
shell脚本自动git svn rebase,推送到github(shell script for automated git svn rebase, push to github)[2022-01-31]
让我们按照步骤输出: 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 ...
-
发布“git svn”回购(Publishing a “git svn” repo)[2023-03-09]
发布SVN分支是安全的,但git-svn dcommit是所有提交都使用git-svn dcommit推送到SVN git-svn dcommit 。 如果你在分支上没有任何变化,那么git-svn rebase只会快速前进。 如果有人从您发布的分支中分支出来,重要的是他们知道它来自SVN回购。 这是因为如果你试图接受他们的改变,推动他们进入SVN回购的唯一方法就是重新调整他们的改变。 在您再次发布提交的更改后,他们将不得不处理冲突的提交,因为哈希将不匹配。 在master工作是安全的,但可能不实际。 从上 ... -
SVN / Git / Bash(SVN / Git / Bash)[2022-09-15]
您已经描述了SubGit的工作原理。 从2.0开始,它允许使用预接收Git钩子(在'git push'上执行)在Git和SVN之间进行双向转换。 要在你的机器上运行 $ subgit configure --svn-urlrepo.git $ #adjust repo.git/subgit/{config,passwd,authors.txt} to set auth options, branches to translate and SVN<->Git ... -
撤消git svn rebase(Undo a git svn rebase)[2023-06-03]
您可以使用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 rebase冲突(git svn rebase conflicts)[2023-07-02]
如果您准备完全忽略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 ...