Article 6KRN9 Problem with char* pointers with GCC/Debian

Problem with char* pointers with GCC/Debian

by
BruceV
from LinuxQuestions.org on (#6KRN9)
I have a console application with a main loop for reading a command string, interpreting it and doing stuff. In essence (irrelevant detail is omitted, this is pseudocode).

While (1)
{
printf (iEnter command.i) ;
getstring (cmdbuf) ;
do stuff.
}
A typical command string will consist of a number of abbreviations, and some numbers, like this:

Mov 23 45 shift 12

The spaces are just common whitespace.

The easiest way to separate the string elements into separate substrings seemed to be to do it in place, simply by finding the first whitespace character after each entry and changing it to \0, and storing the pointer address of each following entry in s char* pointer array. So I end up with an array of pointers to each entry, ie:

Char* Entry[0] is Mov
Char* Entry[1] is 23
Etc.

I ran into trouble once this was set up and I started doing things with my isubstringsi. For example, the following statement causes a segmentation fault.

Num = atoi (Entry[1]) ;

I have confirmed that Entry 1 is i23i, with a \0 terminator. The Entry[] array is correctly declared. The command string memory is reserved with malloc(). My application worked fine until I implemented this 'in place' command string partitioning.

Can anyone see what's wrong?
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments