LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-01-2024, 08:48 AM   #1
BruceV
LQ Newbie
 
Registered: Jan 2016
Posts: 22

Rep: Reputation: Disabled
Problem with char* pointers with GCC/Debian


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 (“Enter command.”) ;
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 ‘substrings’. For example, the following statement causes a segmentation fault.

Num = atoi (Entry[1]) ;

I have confirmed that Entry 1 is “23”, 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?
 
Old 04-01-2024, 04:00 PM   #2
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,150

Rep: Reputation: 393Reputation: 393Reputation: 393Reputation: 393
I can't picture what you are doing the way you describe it. Seems over complicated. strtok is a better choice. in the standard lib.

Here is my implementation of what I think you are describing. My stuff is BSD2.0 Licensed.

Code:
char *
string_field_search(const char * str,
                    const char * delimiter,
                    int field)
{
  assert(str); assert(delimiter);
  
  // declare & initialize
  char * retval = NULL;
  char * token = NULL;
  int counter = 0;
  char * buffer = strdup(str);

  // tokenize and iterate till field found
  do {
    token = strtok(buffer,
                   delimiter);
    if (!token) {
      break;
    }
    while (token) {

      // if proper value iterate until field
      if (counter == field) {
        retval = strdup(token);
        if (retval) {
          retval[strcspn(retval,
                       "\n")] = '\0';
        }
        token = NULL;
        break;
      } else {
        
        // else continue to next token until found
        counter++;
        token = strtok(NULL,
                       delimiter);
      }
    }
  } while (counter < field);

  // clean up and return
  free(buffer); buffer = NULL;
  return(retval);
}
 
  


Reply

Tags
debian 12, gcc 13, pointers



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Freeing pointers to pointers devnull10 Programming 24 07-26-2012 04:58 AM
[SOLVED] Could I get some pointers on pointers please? theKbStockpiler Programming 46 05-18-2010 12:30 AM
C pointers confusion - char ** = char [][] ?? saravkrish Programming 12 12-02-2004 10:06 AM
Pointers Pointers Pointers urzumph Programming 9 03-11-2004 09:49 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration