C inserting a string into a char pointer

Hi everyone, I'm trying to assign string to a char * pointer. Below is how I do it, but I get this warning : assignment makes integer from pointer without a cast.

What is the correct solution to string problems in C?

 char* protoName = ""; if(h->extended_hdr.parsed_pkt.l3_proto==0) *protoName = "HOPOPT"; else if(h->extended_hdr.parsed_pkt.l3_proto==1) *protoName = "ICMP"; else if(h->extended_hdr.parsed_pkt.l3_proto==2) *protoName = "IGMP"; else if(h->extended_hdr.parsed_pkt.l3_proto==3) *protoName = "IGMP"; else if(h->extended_hdr.parsed_pkt.l3_proto==4) *protoName = "IGMP"; char all[500]; sprintf(all,"IPv4','%s'",* protoName); 
+4
source share
5 answers

If you just want to change which string literal protoName points to, you just need to change

 *protoName = "HOPOPT"; 

to

 protoName = "HOPOPT"; 

*protoName = tries to write the first character pointed to by protoName . This will not work in your case, since protoName points to a string literal that cannot be changed.

You also need to change your sprintf call to

 sprintf(all,"IPv4','%s'", protoName); 

The %s format specifier signals that you will pass a pointer to a char array with zero completion. *protoName gives the character code of the first character pointed to by protoName ; sprintf does not know this, so it would process this character code as the address of the array to read from. You do not own this memory, so the effects of reading from it will be undefined; likely to fail.

As an aside, if you have a writable char array and you want to change its contents, you will need to use strcpy to copy a new array of characters into it.

+8
source

If you use constants, just move the pointer, not the contents:

 const char* protoName = ""; if(h->extended_hdr.parsed_pkt.l3_proto==0) protoName = "HOPOPT"; else if(h->extended_hdr.parsed_pkt.l3_proto==1) protoName = "ICMP"; else if(h->extended_hdr.parsed_pkt.l3_proto==2) protoName = "IGMP"; else if(h->extended_hdr.parsed_pkt.l3_proto==3) protoName = "IGMP"; else if(h->extended_hdr.parsed_pkt.l3_proto==4) protoName = "IGMP"; 
+2
source

Here are some examples:

 #include <stdio.h> const char * protoNameFromPktId(int id) { static char* protoName[] = { "HOPOPT", "ICMP", "IGMP", "IGMP","IGMP"}; return protoName[id]; } main() { printf("%s\n", protoNameFromPktId(2)); char all[500]; sprintf(all,"%s", protoNameFromPktId(2)); strcpy(all, protoNameFromPktId(2)); } 
+2
source

You just need to do the following: -

 protoName = "HOPOPT"; 

instead

 *protoName = "HOPOPT"; 

So change as: -

 char* protoName = ""; if(h->extended_hdr.parsed_pkt.l3_proto==0) protoName = "HOPOPT"; else if(h->extended_hdr.parsed_pkt.l3_proto==1) protoName = "ICMP"; else if(h->extended_hdr.parsed_pkt.l3_proto==2) protoName = "IGMP"; else if(h->extended_hdr.parsed_pkt.l3_proto==3) protoName = "IGMP"; else if(h->extended_hdr.parsed_pkt.l3_proto==4) protoName = "IGMP"; char all[500]; sprintf(all,"IPv4','%s'",* protoName); 
+1
source

The main problem I see is that you are not binding memory to strings, so you need to use malloc or better use strdup , which automatically allocates memory. because if you assigned large lines, then you should have a problem. The problem with the warning received from others is in order. plz correct me if i am wrong.

  char * protoName;
 if (h-> extended_hdr.parsed_pkt.l3_proto == 0) {
    protoName = strdup ("HOPOPT");    
 }
 else if (h-> extended_hdr.parsed_pkt.l3_proto == 1) {
   protoName = strdup ("ICMP");  
 ...
 char all [500];
 sprintf (all, "IPv4 ','% s'", * protoName);
 free (protoName);
+1
source

Source: https://habr.com/ru/post/1499434/


All Articles