Reputation: 275
Hi all I trying to assign string
into a char *
pointer. Below is how I am doing but I am getting this warning: assignment makes integer from pointer without a cast.
What is the correct why to deal with strings 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);
Upvotes: 5
Views: 3810
Reputation: 3167
Here's 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));
}
Upvotes: 2
Reputation: 2568
The main problem that I can see is that you dont bind memory for the strings, so you have to use malloc
or better use strdup function that allocates memory automatically. because if you assigned big strings then you should have a problem.
The problem with the warning answered by the others, so is ok. plz correct me if I'm 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);
Upvotes: 1
Reputation: 42165
If you just want to change which string literal protoName
points to, you just need to change
*protoName = "HOPOPT";
to
protoName = "HOPOPT";
*protoName =
attempts to write to the first character pointed to by protoName
. This won't work in your case as protoName
points to a string literal which cannot be modified.
You also need to change your sprintf
call to
sprintf(all,"IPv4','%s'", protoName);
The %s
format specifier signals that you'll be passing a pointer to a nul-terminated char
array. *protoName
gives you the character code of the first character pointed to by protoName
; sprintf
doesn't know this so would treat that character code as the address of the array to read from. You don't own this memory so the effects of reading from it would be undefined; a crash would be likely.
As an aside, if you had a writeable char
array and wanted to change its contents, you'd need to use strcpy
to copy a new array of chars into it.
Upvotes: 8
Reputation: 172418
You just need to do this:-
protoName = "HOPOPT";
instead of
*protoName = "HOPOPT";
So change like :-
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);
Upvotes: 1
Reputation: 122391
If you are using constants, then simply reassign 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";
Upvotes: 2