0

I'm making a code to remove the file name and type from a path. However, i'm receiving warnings concerning the line where i change the content from a character. How could i get rid of the warning?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *getPath(char *fullPath){
    char *aux;
    int a, b, c;    
    
    
    aux = malloc(50 * sizeof(char));        
    aux = fullPath;
    
    
    a = strlen(aux);    
    
    
    for(b=0; b<a; b++){
        if (aux[b] == '/'){
            c = b;
        }
    }
    
    
    for(c; c < a; c++){
///PROBLEM HERE
        aux[c] = "";                
    }   
///PROBLEM HERE 
    return aux;
}



int main(void) {
    
    char C[50];
    char *path, *filename;
    scanf("%s", C);
    
    path = getPath(C);
    
    printf("%s", path);
    
}
6
  • 2
    aux[c] is a single character yet you assign it a string, this is not possible Commented Jan 27, 2021 at 3:02
  • 2
    Felipe what do you think aux = malloc(50 * sizeof(char)); aux = fullPath; does? Commented Jan 27, 2021 at 3:02
  • 2
    also, why do you call malloc then immediately discard the result (by reassigning aux) ? Commented Jan 27, 2021 at 3:02
  • 2
    another problem is that c is used uninitialized if there was no / Commented Jan 27, 2021 at 3:03
  • 1
    You have a major memory leak — you throw away the pointer to the allocated memory when you assign aux = fullPath;. You should probably be using strcpy(), and using strlen(fullPath) + 1 rather than 50. Commented Jan 27, 2021 at 3:05

2 Answers 2

1
aux[c] = ""; // here "" is a char *

aux is a char *, therefore aux[c] is a char (not a string "")

aux[c] = '\0'; 

As written in the comments, there still have other mistakes in the rest of the code: for example aux value is erased.

Sign up to request clarification or add additional context in comments.

1 Comment

I solved this malloc mistake, and changed the "" for ''. I didn't know that "" was for strings and '' for characters, thought it was the same.
0

Tried fixing the entire code.

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

bool getPath(char *const strippedPath, const int strippedPath_buflen,const char *const fullPath){
  int b=strlen(fullPath);
  for(;;){
    --b;
    if(b<0)
      return false;
    if('/'==fullPath[b])
      break;
  }
  if(strippedPath_buflen<b+1)
    return false;
  strncpy(strippedPath,fullPath,b);
  strippedPath[b]='\0';
  return true;
}

int main(void) {
  for(;;){
    char C[50]={};
    printf("> ");
    fflush(stdout);
    scanf("%s",C);
    if(0==strcmp("quit",C))
      break;
    char path[3+1]={'X','X','X','X'};
    if(getPath(path,4,C))
      printf("%s\n",path);
    else
      printf("err\n");
  }
  return 0;
}
> aaaa/b.txt
err
> aaa/b.txt
aaa
> a/c/b.txt
a/c
> aa/b.txt
aa
> a/b.txt
a
> a/
a
> /b.txt

> b.txt
err
> quit

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.