Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

arrays - "Warning: assignment makes integer from pointer without a cast" using a char malloc

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);
    
}
question from:https://stackoverflow.com/questions/65912294/warning-assignment-makes-integer-from-pointer-without-a-cast-using-a-char-mal

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
aux[c] = ""; // here "" is a char *

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

aux[c] = ''; 

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...