Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9011749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:49:09+00:00 2026-06-16T02:49:09+00:00

Alright. So I have this program in C where I take a string with

  • 0

Alright. So I have this program in C where I take a string with arguments and values (Such as: "GO 45 STOP 15"). The goal is to parse the string and place the argument with its corresponding value into a typedef structure to work with later on.

This is what my structure looks like:

typedef struct {
    char* keyword;
    double value;
} parameter;

Here are some copies of my code that I am having issues with.
Both main() and initParams() are in the same file and therefore both have access to the same #defines…

main():

#include <stdio.h>
#include <stdlib.h>
#include "findArgs.h"

#define STR0_SIZE 80
#define LIST_SIZE   4
#define MAX_PARAMS 15

void main(){
int i;

char str0[STR0_SIZE] = "LEFT 45 GO 686 GO 34.3 STOP 56 RIGHT 26";   //Input String
char* wordList[LIST_SIZE]={"GO", "STOP", "LEFT", "RIGHT"};

int num_arg = 0; //Number of arguements in str0 (each space denotes the end of an arg)

parameter* param;
initParams(param);

replaceSpaces(str0, &num_arg);

findWords(param, str0, num_arg);
}

initParams:

void initParams(parameter* param){
int ctr0, ctr1;

param = (parameter*) malloc(MAX_PARAMS * sizeof(parameter));
printf("\n%i\n", sizeof(parameter));
for(ctr0=0;ctr0<MAX_PARAMS;ctr0++){
        param[ctr0].keyword = "";
        param[ctr0].value = 0;
}

}
ok some quick explainations. initParams is for allocating the memory for each of my parameters. I am assuming that I will not have any idea how many parameters will be included in the string and plan on determining the number in the string later in the program. I do know that I will not accept more than parameters in the string.
After allocating the memory, I loop through each one and initialize each value to either an empty string or 0. (I do realize this is probably unnecessary, however I have done this as part of my code troubleshooting.

Continuing on, replaceSpaces() simply loops through the string and replaces each occurrence of ‘ ‘ with a ‘\0’. It also counts the number of arguements present in the string so that I know how many new strings I have just created by adding null terminators.

Now the tricky part in which I am having difficulty.

#define MAX_ARG_LENGTH 20

void findWords(parameter* param, char* str0, int num_arg){
parameter temp[countWords(str0, num_arg)];
int i;
int ctr0,ctr1, ctr2=0;
int word=0; //flag
char tempStr[MAX_ARG_LENGTH]="";
char* c0 = str0;

for(ctr0=0; ctr0<num_arg; ctr0++){
    word=0;
    ctr1=0;
    if(((*c0 > 'a') && (*c0 <'z')) || ((*c0 > 'A') && (*c0 <'Z'))){
        word=1;
        tempStr[ctr1]=*c0;
        ctr1++;
    }
    while(*c0 != '\0'){
        c0++;
        if(word)
            tempStr[ctr1++] = *c0;
        printf("\ntempStr: '%s'\n", tempStr);
    }
    if(word){
        param[ctr2].keyword = tempStr;
        printf("%s\n", param[ctr2].keyword);
        ctr2++;
    }
    c0++;
}
    for(i=0; i<num_arg/2;i++){
        printf("'%s'\n", param[i].keyword);
        printf("'%g'\n", param[i].value);
}


}

This function works properly at finding each word in the string and storing it in tempStr. My issue is with assigning it to my parameter array back in my main(). I have tried assigning them to a temp array of parameters and then setting "param" equal to the temp array. However due to the fact that param is a pointer, when I assign it a local location, after the function is finished, the memory is freed and lost. Enter my idea to use malloc to predefine memory for them and assign it after.

This is my current output when I compile and run the code. I added comments to clarify

16 //sizeof(parameter)

5 //Number of words in str0

LEFT

GO

GO

STOP

RIGHT

‘RIGHT’ //Things in single quotes are prints from the array of parameters

‘8.05316e-315’

‘RIGHT’

‘0’

‘RIGHT’

‘8.04051e-315’

‘RIGHT’

‘0’

‘RIGHT’

‘0’

MAIN: //I printed these in main using the same for loop seen in findWord()

‘▒▒’

‘8.05316e-315’

‘▒▒’

‘0’

‘▒▒’

‘8.04051e-315’

‘▒▒’

‘0’

‘▒▒’

‘0’

If anyone can help me properly assign the contents of tempStr to a parameter in my array declared in main I would be greatly appreciative. Please Let me know if you need any more information.

THANK YOU ALL!! I GOT IT!!!

Rather than make "tempStr" I just assgned the characters directly to the param[index].keyword. It worked like a charm

Thank you guys very much! I had read many different Qs and As here before but this was my first time posting. I am very excited with how quickly you guys were able to reply.

Thanks again!

~Nick

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T02:49:11+00:00Added an answer on June 16, 2026 at 2:49 am

    I think you are misunderstanding what param[ctr2].keyword = tempStr; does It does not copy the string in tempstr to keyword it just makes keyword point to tempstr, which means all the keywords will point to the tempStr variable and will be invalid if you access it outside this function.

    What you want to do is to make

    char* keyword;
    

    into

    char keyword[MAX_ARG_LENGTH];
    

    And use something like strcpy or strncpy to do the copying.

    You also do not appear to be setting value anywhere

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright, I have this program to sparse code in Newick Format, which extracts both
Alright, so I have this collision detection code, and I'm trying to push the
Alright, so I have a block being drawn at 0,0,0 and I have this
Alright so I have no idea how to even begin doing this But basically
Alright this is my first day with JQuery so have fun with these functions
Alright I know this is more than likely a amateur question but I have
Alright, this one's interesting. I have a solution, but I don't like it. The
Alright, this is melting my brain. It might have something to do with the
Alright, this problem is a little complicated, so bear with me. I have a
Alright, so I'm writing this program that essentially batch runs other java programs for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.