I have such a structure:
typedef struct kodProgramu {
char* etykieta;
char* instrukcja;
char* operand;
struct kodProgramu *nast;
} kodPrg;
This code is for adding new element:
void pobierzKodStdin(kodPrg *kod, char *wynik, char *linia, int flagaEtyk)
{
wynik = fgets(linia, 80, stdin);
while(wynik != NULL)
{
kodPrg *wsk, *nowy;
wsk = kod;
while(wsk->nast != NULL)
wsk = wsk->nast;
if(linia[0] == ' ')
flagaEtyk = 1;
nowy = (kodPrg*)malloc(sizeof(kodPrg));
int licznik = 0;
char *pch;
pch = strtok(linia, ":# ");
while(pch != NULL)
{
if(flagaEtyk == 0)
{
if(licznik == 0)
nowy->etykieta = pch;
else if(licznik == 1)
nowy->instrukcja = pch;
else if(licznik == 2)
nowy->operand = pch;
}
if(flagaEtyk == 1)
{
if(licznik == 0)
nowy->instrukcja = pch;
else if(licznik == 1)
nowy->operand = pch;
}
licznik++;
pch = strtok(NULL, ":# ");
}
nowy->nast = NULL;
wsk->nast = nowy;
flagaEtyk = 0;
wynik = fgets(linia, 80, stdin);
}
}
This function print this structure to the console:
void wypiszKod(kodPrg *kod)
{
kodPrg *wsk = kod;
while(wsk != NULL)
{
printf("%s %s %s\n", wsk->etykieta, wsk->instrukcja, wsk->operand);
wsk = wsk->nast;
}
}
This is my main function:
int main()
{
char linia[80], *wynik;
char *wsk = malloc(sizeof(char));
int flagaEtyk = 0;
//tasmaWejsc *wejscie = (tasmaWejsc*)malloc(sizeof(tasmaWejsc));
//tasmaWyjsc *wyjscie = (tasmaWyjsc*)malloc(sizeof(tasmaWyjsc));
//wyjscie->wartosc = 0;
//wyjscie->nast = NULL;
kodPrg *kod = (kodPrg*)malloc(sizeof(kodPrg));
kod->etykieta = " ";
kod->instrukcja = " ";
kod->operand = " ";
kod->nast = NULL;
int liczba;
//wprowadzWejscie(wynik, linia, wejscie);
//wypiszWejscie(wejscie);
//system("cls");
pobierzKodStdin(kod, wynik, linia, flagaEtyk);
wypiszKod(kod);
return 0;
}
Now, when I enter one line like : test test test
It's working good and print test test test in console.
But when I enter more lines, for example:
test test test
xxxx xxxx xxxx
The program is printing:
xxxx xxxx xxxx
xxxx xxxx xxxx
It's like the second line replace the first one.
I don't know why, when I have a struct with int instead of char * it's working good. Next element are added and it' printing good, but when char * it's working as I described above.
How to add new elemnt to the list when I have struct with char *?
I think you should realize that strtok works on a static buffer
So when you write like this
pch = strtok(linia, ":# ");
while(pch != NULL)
{
if(flagaEtyk == 0)
{
if(licznik == 0)
nowy->etykieta = pch;
you are assigning the pointer in your shiny heap element to a string that will disappear by the next line (linia).
wynik = fgets(linia, 80, stdin);
what you need to do is to make a copy of the string, this can be done using strdup()
nowy->etykieta = strdup(pch);
Every string in your linked list is mapped to portions of linia[80], which is overwritten at each line access.
Some strdup() calls should solve your issue.
Related
So I have a struct that I'm declaring in a function and whenever I try running the program it says that the variable is undeclared. However, that error only appears in that function and it doesn't appear in any other function when I declare that struct.
https://repl.it/#JeevenMann/RelevantWetInverse#main.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct wordStruct {
char *charPointer;
int numChars;
int wordPos;
struct wordStruct *nextWord;
} word;
typedef struct sentenceStruct{
word *wordObj;
int lineNum;
int wordCount;
struct sentenceStruct *nextSentence;
} sentence;
typedef struct linkedStructure {
sentence *firstSentence;
int sentenceNum;
} LIST;
void printOut(LIST * sentenceList) {
//Declare variables for sentenceIndex
int sentenceIndex = 0;
//reset sentence Index and print out all words on each line
sentenceIndex = 0;
sentence *currentSentence = sentenceList->firstSentence;
printf("\n\nThis is all the words printed out on the same line as entered:\n");
while (currentSentence != NULL) {
word *currentWord = currentSentence->wordObj;
int wordIndex = 0;
while (currentWord != NULL) {
printf("%s ", currentWord->charPointer);
currentWord = currentWord->nextWord;
}
printf("\n");
currentSentence = currentSentence->nextSentence;
}
}
void storeWords(sentence * sentenceObj, char line[]) {
//Declare count variables and index variables
int index = 0;
int wordCount = 1;
int charCount = 1;
word* currentWord;
char copyWord[1000];
//Delegate memory to wordObject and char array
sentenceObj -> wordObj = (word * ) malloc(sizeof(word));
currentWord = sentenceObj->wordObj;
// Loop though sentence until we reach EOS char
while (line[index] != '\n') {
//check if array index is not a space
if (line[index] != ' ') {
//set char to current index
//increase char count
//realloc more memory to char pointers
copyWord[charCount - 1] = line[index];
charCount++;
//if the next index is a EOS symbol or an empty space
if (line[index + 1] == '\n' || line[index + 1] == ' ') {
sentenceObj -> wordCount = wordCount;
currentWord->numChars = charCount - 1;
currentWord->wordPos = wordCount;
copyWord[charCount-1] = '\0';
currentWord->charPointer = (char * ) malloc(sizeof(char) * charCount);
strcpy(currentWord->charPointer,copyWord);
if (line[index + 1] == ' ') {
wordCount++;
charCount = 1;
currentWord->nextWord = (word * ) malloc(sizeof(word));
currentWord = currentWord->nextWord;
currentWord->nextWord = NULL;
strcpy(copyWord,"");
}
}
}
index++;
}
}
LIST * readInput() {
//declare variables for str and lineNum
char str[1000] = "";
int lineNum = 1;
LIST *sentenceList = (LIST *) calloc(1,sizeof(LIST));
sentence *currentSentence;
sentenceList->firstSentence = (sentence * ) calloc(1, sizeof(sentence));
currentSentence = sentenceList->firstSentence;
//while the user doesn't enter an empty string, continue
printf("Enter a string: ");
fgets(str,sizeof(str),stdin);
printf("%s\n",str );
while (strcmp(str, "\n") != 0){
//user hasn't entered an empty string so allocate more memory and populate
//the struct
currentSentence->lineNum = lineNum;
storeWords(currentSentence, str);
lineNum = lineNum + 1;
printf("Enter a string: ");
fgets(str,sizeof(str),stdin);
printf("%s\n",str );
if (strcmp(str, "\n") != 0) {
currentSentence->nextSentence = (sentence * ) calloc(1, sizeof(sentence));
currentSentence = currentSentence->nextSentence;
currentSentence->nextSentence = NULL;
}
}
//print out the sentence as defined by assignment
printOut(sentenceList);
return sentenceList;
}
int searchSentences(LIST * sentenceList, char word[]) {
//Declare variables for the sentence index and return int boolean if word is found
int sentenceIndex = 0;
int wordFound = 0;
//loop through sentenceObjects and print out if that word matches word to find
sentence *currentSentence = sentenceList->firstSentence;
printf("\n\nThis is all the words printed out on the same line as entered:\n");
while (currentSentence != NULL) {
word *theWord = currentSentence->wordObj;
int wordIndex = 0;
while (theWord != NULL) {
if (strcmp(theWord->charPointer,word) == 0) {
printf("%s found in line %d position %d\n", word, currentSentence->lineNum, theWord.wordPos);
wordFound = 1;
}
theWord = theWord->nextWord;
}
printf("\n");
currentSentence = currentSentence->nextSentence;
}
return wordFound;
}
void findString(LIST * sentenceList) {
//declare char array
char str[1000];
//while the user doesn't enter an empty string continue to search for the word
do {
printf("\n\nEnter a word to search for:\n");
fgets(str,sizeof(str),stdin);
str[strlen(str)-1] = '\0';
if (strcmp(str, "") != 0 && searchSentences(sentenceList, str) == 0) {
printf("%s was not found in this file\n", str);
}
} while (strcmp(str, "") != 0);
}
int main() {
//Declare line objects
LIST *sentenceList;
//get Number of lines and ask user for info
sentenceList = readInput();
findString(sentenceList);
return 0;
}
Thanks, I've been trying to fix this for a while now, but I can't seem to figure out why it's not working.
I using strtok() to split a string and store in an array like following below
char *table[5];
char buffer[50] = {"1-Study"}; // The value is example, get new value by user
char *number;
char *name;
uint8_t tableNumber;
number = strtok(buffer, "-"); //equals "1"
name = strtok(NULL, "-"); //equals "Study"
tableNumber = atoi(number); //convert char to int
table[tableNumber] = name;
for (c = 0; c < 5; c++)
{
printf("table %d = %s\n", c, table[c]);
}
after get input for 5 times the result should be:
table 0 = Study
table 1 = Sleep
table 2 = Party
table 3 = Hello
table 4 = Exit
But the resualt is:
table 0 = Exit
table 1 = Exit
table 2 = Exit
table 3 = Exit
table 4 = Exit
whats the problem?
please help me?
Thanks
complete code:
char gMessageBuffer[40];
char *gSceneTable[13];
boolean emberAfPreMessageReceivedCallback(EmberAfIncomingMessage* incomingMessage)
{
if (incomingMessage->apsFrame->profileId == HA_PROFILE_ID)
{
if (incomingMessage->apsFrame->clusterId == ZCL_SCENES_CLUSTER_ID)
{
MEMCOPY(gMessageBuffer, incomingMessage->message, incomingMessage->msgLen); // Get incoming message
gMessageBuffer[incomingMessage->msgLen] = '\0';
emberEventControlSetDelayMS(getScenePayloadEventControl, SCENE_ACTION_TRESH);
return true;
}
}
return false;
}
void getScenePayloadEventFunction(void)
{
char *sceneNumber;
char *sceneName;
char *sceneID;
char *sceneAction;
uint8_t sceneTableNumber;
emberAfCorePrintln("///Incoming Message: %s///", gMessageBuffer);
sceneNumber = strtok(gMessageBuffer, ".");
sceneName = strtok(NULL, ".");
sceneID = strtok(NULL, ".");
sceneAction = strtok(NULL, ".");
emberAfCorePrintln("///SCENE NUMBER: %s///", sceneNumber);
emberAfCorePrintln("///SCENE NAME: %s///", sceneName);
emberAfCorePrintln("///SCENE ID: %s///", sceneID);
emberAfCorePrintln("///SCENE ACTION: %s///", sceneAction);
if (strcmp(sceneAction, "Update") == 0)
{
sceneTableNumber = atoi(sceneNumber);
gSceneTable[sceneTableNumber] = strdup(sceneName);
}
emberEventControlSetInactive(getScenePayloadEventControl);
}
this is for microcontroller in simplicity studio IDE.
I get payload in emberAfPreMessageReceivedCallback correctly
and I split it into 4 parts and print correctly too.
But after copy the sceneName to gSceneTable array I see the last sceneName in all the elements of gSceneTable with gSceneTable[sceneTableNumber] = sceneName and I see "p]" with gSceneTable[sceneTableNumber] = strdup(sceneName);
It is highly unlikely that your program produce the posted output. The code fragment only handles a single string and char *table[5]; is uninitialized, so printing the strings from table[0], table[2], table[3] and table[4] has undefined behavior. You specified that the strings are read from a file, posting a complete program is required for a precise and correct analysis. Not initializing the array is a problem in case the file does not have all entries covered, it would be impossible to tell which were set and which weren't.
Assuming your program reads the strings from a file or standard input, parsing them with strtok returns pointers to the source string, which is the array into which you read the lines from the file. Hence all entries in the table[] array point to the same byte in this array, which explains the output you get: 5 times the contents of the last line.
You should make a copy of the string you store in the table:
table[tableNumber] = strdup(name);
Here is a completed and modified program:
#include <stdio.h>
#include <string.h>
int main() {
char *table[5] = { NULL, NULL, NULL, NULL, NULL };
char buffer[50];
char *number;
char *name;
int tableNumber;
for (int i = 0; i < 5; i++) {
if (!fgets(buffer, sizeof buffer, stdin))
break;
number = strtok(buffer, "-");
if (number == NULL) {
printf("empty line\n");
continue;
}
name = strtok(NULL, "-\n");
if (name == NULL) {
printf("no name after -\n");
continue;
}
tableNumber = atoi(number);
if (tableNumber < 0 || tableNumber >= 5) {
printf("invalid number: %d\n", tableNumber);
continue;
}
table[tableNumber] = strdup(name);
}
for (int i = 0; i < 5; i++) {
if (table[i])
printf("table %d = %s\n", i, table[i]);
}
for (int i = 0; i < 5; i++) {
free(table[i]);
}
return 0;
}
If your target system does not support strdup(), use this:
#include <stdlib.h>
#include <string.h>
char *mystrdup(const char *s) {
size_t size = strlen(s) + 1;
char *p = malloc(size);
return (p != NULL) ? memcpy(p, s, size) : NULL;
}
The sample code:
Enter message like "sceneNumber.sceneName.sceneID.Update"
For example: 1.Study.12345.Update
#include <stdio.h>
#include <string.h>
#include <conio.h>
char *gSceneTable[13];
char gMessageBuffer[50];
int main()
{
char *sceneNumber;
char *sceneName;
char *sceneID;
char *sceneAction;
int sceneTableNumber;
int check;
int c;
printf("Enter payload for 3 Times\r\n");
while(check != 3)
{
scanf("%s", &gMessageBuffer);
printf("Message is: %s\r\n",gMessageBuffer);
sceneNumber = strtok(gMessageBuffer, ".");
sceneName = strtok(NULL, ".");
sceneID = strtok(NULL, ".");
sceneAction = strtok(NULL, ".");
printf("%s\r\n", sceneNumber);
printf("%s\r\n", sceneName);
printf("%s\r\n", sceneID);
printf("%s\r\n", sceneAction);
if (strcmp(sceneAction, "Update") == 0)
{
sceneTableNumber = atoi(sceneNumber);
gSceneTable[sceneTableNumber] = sceneName;
}
check++;
}
for (c = 0; c < 4; c++)
{
printf("Scene Table: %d ----- %s \r\n", c, gSceneTable[c]);
}
return 0;
}
while(token != NULL)
{
// for(position = strcspn(str,token); position >= 0;
// position = strcspn(str, token + 1));
// {
// str2[position] = count++;
// }
}
I think I'm having a logic issue with my code. I'm trying to take in a string from user input and return how many times each word was used and only return each word one time. I think my issue is within the section I have commented out but I'm not entirely sure how to fix or change my code.
For example:
Input: Hello, my cat is saying Hello.
Output: Hello 2
my 1
cat 1
is 1
saying 1
I have modified your code and written little differently,
Please have a look.
int main()
{
char haystack[50] = "Hello my cat is saying Hello";
char needle[10];
int i = 0,j = 0,k = 0;
char *ret = NULL;
int cnt = 0;
while(haystack[i] != NULL)
{
if(haystack[i] == ' ')
{
i++;
}
else
{
//Get each sub strings.
while((haystack[i] != ' ') && (haystack[i] != NULL))
{
needle[k++] = haystack[i];
i++;
}
needle[k] = '\0';
printf("The substring is: %s", needle);
//Find how many times the sub string is there in the string
while(strstr(haystack, needle) != NULL)
{
ret = strstr(haystack, needle);
//Once the Substring is found replace all charecter of that substring with space.
for(j=0;j<k;j++)
{
*(ret+j) = ' ';
}
cnt++;//Count the no of times the substrings found.
}
printf("= %d\n",cnt);
cnt = 0;
k = 0;
}
}
return(0);
}
I have not taken care for the special characters, You can modify to take care of those.
So I have used the string "Hello my cat is saying Hello" instead of "Hello, my cat is saying Hello.". Removed the Comma.
Hope this Helps.
To compute how many times a word is present in a string or line you need a structure to preserve all the different words you have and mainly how many times each word is frequent.
My simple approach, without any optimization, is:
#include <stdio.h>
#include <stdlib.h>
struct wordsDetail
{
char word[100];
int freq;
} wordsDetail;
void updateWords(struct wordsDetail s[], int length, char *token)
{
int i;
for (i = 0; i < length && s[i].word[0] != '\0'; i++) {
if (strcmp(s[i].word, token) == 0) {
s[i].freq++;
return;
}
}
strcpy(s[i].word, token);
s[i].freq++;
}
void printResults(struct wordsDetail s[], int length) {
printf("Words\tFreq\n");
for (int i = 0; i <length && s[i].word[0] != NULL; i++) {
printf("%s\t%d\n", s[i].word, s[i].freq);
}
}
int main(void)
{
struct wordsDetail myWords[100];
int wordsDetailLength = sizeof(myWords) / sizeof(wordsDetail);
const size_t line_size = 1024;
char *str = NULL;
int *str2 = NULL;
int i = 0;
char *token;
for (i = 0; i < wordsDetailLength; i++) {
myWords[i].word[0] = '\0';
myWords[i].freq = 0;
}
if ((str = calloc(line_size, sizeof(char))) == NULL) {
printf("error\n");
exit(-1);
}
printf("Input: ");
if (scanf("%[^\n]", str) != 1) {
printf("error\n");
exit(-1);
}
printf("Output: \n");
token = strtok(str, " .,!");
while (token != NULL) {
updateWords(myWords, wordsDetailLength, token);
token = strtok(NULL, " .,!");
}
printResults(myWords, wordsDetailLength);
return 0;
}
A simple result is:
Input: Hello, my cat is saying Hello to my cat.
Output:
Words Freq
Hello 2
my 2
cat 2
is 1
saying 1
to 1
I have a problem with a code I'm checking. I get a Segmentation fault (core dumped) and I think the problem is in this part of the code
The code supposed to add a new item to a connected list by users input.
The input should look like this
word_#_1999_#_synonym_#_FORIGEN
thank you in advance
// Add a new word to the dictionary with the format of { devoted_#_2003,_2001,_2008_#_worship_#_AHAVA }
struct Word * addWord(struct Word * dictionary)
{
struct Word *scan = dictionary;
struct Word *newWord = (struct Word *)malloc(sizeof(struct Word));
char *input = (char *)malloc(sizeof(char) * 128);
char *inputBackup = (char *)malloc(sizeof(char) * 128);
char *years = (char *)malloc(sizeof(char) * 128);
int count = 0;
int tempYear;
char *wordOfNewWord;
printf("Please enter a new word into dictionary\n");
scanf("%s", input);
strcpy(inputBackup, input);
// Init newWord
newWord = (struct Word *)malloc(sizeof(struct Word));
newWord->next = NULL;
newWord->numOfYears = 0;
// Check if good
if(countSubstring(input, "_#_") != 3)
{
printf("error\n");
return NULL;
}
// Get the word name
wordOfNewWord = strtok(input, "_#_");
newWord->word = (char *)malloc(sizeof(wordOfNewWord));
strcpy(newWord->word, wordOfNewWord);
// Get the word years
years = strtok(NULL, "#");
newWord->numOfYears = countSubstring(years, ",_") + 1;
newWord->years = (unsigned short *)malloc(sizeof(unsigned short) * newWord->numOfYears);
years = strtok(years, ",_");
tempYear = strtol(years, NULL, 10);
if (tempYear <= 9999 && tempYear > 0)
{
*(newWord->years + count) = tempYear;
}
else
{
printf("error\n");
return NULL;
}
count = 1;
years = strtok(NULL, ",_");
while (years != NULL)
{
tempYear = strtol(years, NULL, 10);
if (tempYear <= 9999 && tempYear > 0)
{
*(newWord->years + count) = tempYear;
}
else
{
printf("error\n");
return NULL;
}
count++;
years = strtok(NULL, ",_");
}
// Get word synonims
strcpy(input, inputBackup);
input = strtok(input, "#");
input = strtok(NULL, "#");
input = strtok(NULL, "#");
newWord->numOfSynonym = countSubstring(input, ",_") + 1;
newWord->synonymWords = (char **)malloc(sizeof(char) * 30 * newWord->numOfSynonym);
input = strtok(input, ",_");
*(newWord->synonymWords) = input;
count = 1;
input = strtok(NULL, ",_");
while (input != NULL)
{
*(newWord->synonymWords + count) = input;
count++;
input = strtok(NULL, ",_");
}
// Get translation
input = (char *)malloc(sizeof(char) * 120);
strcpy(input, inputBackup);
input = strtok(input, "#");
input = strtok(NULL, "#");
input = strtok(NULL, "#");
input = strtok(NULL, "#");
newWord->numOfTrans = countSubstring(input, ",_") + 1;
newWord->tranWords = (char **)malloc(sizeof(char) * 30 * newWord->numOfTrans);
input = strtok(input, ",_");
*(newWord->tranWords) = input;
count = 1;
input = strtok(NULL, ",_");
while (input != NULL)
{
*(newWord->tranWords + count) = input;
count++;
input = strtok(NULL, ",_");
}
// Put the new word in the dictionary
if(findWord(dictionary, newWord->word) == 1)
{
printf("error\n");
return NULL;
}
}
there is the struct
struct Word
{
char *word;
unsigned short * years;
unsigned short numOfYears;
char ** synonymWords;
unsigned short numOfSynonym;
char ** tranWords;
unsigned short numOfTrans;
struct Word *next;
};
For a kickoff, this code doesn't make much sense:
if (tempYear <= 9999 && tempYear > 0)
{
*(newWord->years + count) = tempYear;
}
considering the only time count is used prior to this line is: int count = 0;
Other than that, you seem to be oblivious to the fact that char ** and char * are not the same thing!:
newWord->synonymWords = (char **)malloc(sizeof(char) * 30 * newWord->numOfSynonym);
//and
newWord->tranWords = (char **)malloc(sizeof(char) * 30 * newWord->numOfTrans);
Which are allocating char *, but at the same time, you're doing:
char *wordOfNewWord;
newWord->word = (char *)malloc(sizeof(wordOfNewWord));
Which is actually allocating memory to hold a pointer, depending on the architecture (32 or 64 bits) a pointer generally requires 4 to 8 times as much memory as a char, of which the size is by definition 1.
Please allocate memory according to the suggestions made in the comments:
char **pointers_to_strings = malloc(10*sizeof(*pointers_to_strings));
for(int i=0;i<10;++i)
pointers_to_strings[i] = calloc(101, sizeof(*pointers_to_strings[i]));
To ensure you're always allocating the right amount of memory, required to accomodate whatever type it will hold.
And of course, no malloc or calloc without a free call:
for (int i=0;i<10;++i)
{
free(pointers_to_strings[i]);
pointers_to_strings[i] = NULL;
}
free(pointers_to_strings);
pointers_to_strings = NULL;
the problem is in this lines
char *wordOfNewWord;
newWord->word = (char *)malloc(sizeof(wordOfNewWord));
strcpy(newWord->word, wordOfNewWord);
Hi guys I got some problems with using a function with a reference to a string as argument. I read that you should use e double pointer for this, but I can't get it to work.
This is (partly) my code.
enum errCode { ERR_NONE = 0, ERR_EMPTY, ERR_FULL, ERR_MEM, ERR_INIT, ERR_COMMAND, ERR_UNDEFINED };
typedef enum errCode ErrCode;
typedef enum {
no = 0, add, del, src, show, exit
} Command;
int main(void) {
char stringval[50];
char stringval2[50];
ErrCode err;
Command currentCommand = no;
printf("Enter a command\n");
if (fgets(stringval, 50, stdin) != NULL) {
char *p;
if ((p = strchr(stringval, '\n')) != NULL)
*p = '\0';
}
ErrHandler(
extractCommand(¤tCommand, stringval, &stringval2)
);
printf("stringval 2 = %s.\n", stringval2);
return 0;
}
ErrCode extractCommand(Command *command, char *inputString, char **outputString) {
char *strTemp;
char *strTemp2;
//Get the first word of the string
strTemp = strtok(inputString, " ");
strTemp2 = strtok(NULL, " ");
*outputString = strTemp2;
//Check if it equals a command
if (strcmp(strTemp, "exit") == 0) {
*command = exit;
return ERR_NONE;
} else if (strcmp(strTemp, "add") == 0) {
*command = add;
return ERR_NONE;
} else if (strcmp(strTemp, "del") == 0) {
*command = del;
return ERR_NONE;
} else if (strcmp(strTemp, "src") == 0) {
*command = src;
return ERR_NONE;
} else if (strcmp(strTemp, "show") == 0) {
*command = show;
return ERR_NONE;
} else {
*command = no;
printf("%s", strTemp);
return ERR_COMMAND;
}
}
This is what my output looks like:
Enter a command
add this is a test
stringval 2 = z˜ˇøÀo‡èK‡èT¯ˇø.
I obviously want to have the second word of the inputted string, but I'm doing something wrong.
Thx for the help!
stringVal2 is not initialised and is never populated: that is reason junk is being printed. There is no need in this case to pass a char**, passing a char* will work. However, this:
outputString = strTemp2;
does not copy the content of strTemp2 to outputString: it makes outputString point to the same address as strTemp2: use strcpy().
A double pointer, char** for example, is commonly passed to a function when that function allocates a new buffer for the argument (which is not the case in the posted code):
char* buf = NULL;
my_alloc(&buf);
void my_alloc(char** p)
{
*p = malloc(10);
}