Happy Labor Day Weekend. I've got a pointer question that I haven't been able to resolve. I'm trying to pass a string to a function which removes "mdata," from the beginning of the string and then returns a pointer to the new string. It ain't werkin... I'm not defining my pointer variables correctly or something.
Code:
#include<stdio.h>
#include<string.h>
char *rmDataForDBmarker(char *stringToClean, char *whatToRemove);
int main()
{
char logToKeep[] = "mdata,"; // this is the key word of the log entry to keep
char *logFinder = "mdata,b1a571,3324 d,123.46,987.99";
char *dataMarkerRmvd;
printf("\n string before the function: %s", logFinder);
dataMarkerRmvd = rmDataForDBmarker(logFinder, logToKeep);
printf("\n back from rmDataForDBmarker(): dataMarkerRmvd = %s", dataMarkerRmvd);
}
char *rmDataForDBmarker(char *stringToClean, char *whatToRemove){ // in this case
printf("\n Now the code is in the function...");
printf("\n The next stringToClean is = %s", stringToClean);
int i, j = 0, k = 0,n = 0;
int flag = 0;
char *neww[100]; // declaring neww as a pointer
for(i = 0 ; stringToClean[i] != '\0' ; i++)
{
k = i;
while(stringToClean[i] == whatToRemove[j])
{
i++,j++;
if(j == strlen(whatToRemove))
{
flag = 1;
break;
}
}
j = 0;
if(flag == 0){
i = k;
}
else{
flag = 0;
}
printf("\nstringToClean[i] = %c", stringToClean[i]);
n++;
printf("\ni=%d n=%d", i, n);
neww[n] = stringToClean[i];
printf("\n neww[n] = %c", neww[n]);
}
neww[n] = '\0';
printf("\n Here's the original string: %s", stringToClean);
printf("\n String with %s removed: %s", whatToRemove, neww);
printf("\n The memory location of neww: %p", neww);
return neww; // trying to return the pointer to neww
}
When I compile it, the build log provides some insight that I don't understand:
Code:
C:\work\arduino\trash\Untitled6.c|46|warning: assignment to 'char *' from 'char' makes pointer from integer without a cast [-Wint-conversion]|
C:\work\arduino\trash\Untitled6.c|53|warning: returning 'char **' from a function with incompatible return type 'char *' [-Wincompatible-pointer-types]|
C:\work\arduino\trash\Untitled6.c|53|warning: function returns address of local variable [-Wreturn-local-addr]|
I can see in the output that the string looks to be written to the "neww" string. The output looks like this:
I'm a novice coder. If someone could point me in the right direction, I'd appreciate it.