const int*p // p所指向的空间是常量 不可修改 ,但p可以修改
int*const p // p所指向的空间是可以修改 ,p不可以修改
#include <stdio.h>
#include <string.h>
struct usb_string {
char id;
const char *s;
};
enum {
STR_ASSOC,
STR_AC_IF,
STR_USB_OUT_IT,
STR_USB_OUT_IT_CH_NAMES,
STR_IO_OUT_OT,
STR_IO_IN_IT,
STR_IO_IN_IT_CH_NAMES,
STR_USB_IN_OT,
STR_AS_OUT_IF_ALT0,
STR_AS_OUT_IF_ALT1,
STR_AS_IN_IF_ALT0,
STR_AS_IN_IF_ALT1,
};
static struct usb_string strings_uac1[] = {
[STR_ASSOC].s = "Source/Sink",
[STR_AC_IF].s = "AC Interface",
[STR_USB_OUT_IT].s = "Playback Input terminal",
[STR_USB_OUT_IT_CH_NAMES].s = "Playback Channels",
[STR_IO_OUT_OT].s = "Playback Output terminal",
[STR_IO_IN_IT].s = "Capture Input terminal",
[STR_IO_IN_IT_CH_NAMES].s = "Capture Channels",
[STR_USB_IN_OT].s = "Capture Output terminal",
[STR_AS_OUT_IF_ALT0].s = "Playback Inactive",
[STR_AS_OUT_IF_ALT1].s = "Playback Active",
[STR_AS_IN_IF_ALT0].s = "Capture Inactive",
[STR_AS_IN_IF_ALT1].s = "Capture Active",
{ },
};
int main()
{
//char buf[] = "qwerrrr";
//strings_uac1[STR_ASSOC].s = (char*)buf;
strcpy(strings_uac1[STR_ASSOC].s,"helloworld");
/* Write C code in this online editor and run it. */
printf("Hello, World! %s\n",strings_uac1[STR_ASSOC].s);
return 0;
}
改为修改指针变量,可以正常运行:
#include <stdio.h>
#include <string.h>
struct usb_string {
char id;
const char *s;
};
enum {
STR_ASSOC,
STR_AC_IF,
STR_USB_OUT_IT,
STR_USB_OUT_IT_CH_NAMES,
STR_IO_OUT_OT,
STR_IO_IN_IT,
STR_IO_IN_IT_CH_NAMES,
STR_USB_IN_OT,
STR_AS_OUT_IF_ALT0,
STR_AS_OUT_IF_ALT1,
STR_AS_IN_IF_ALT0,
STR_AS_IN_IF_ALT1,
};
static struct usb_string strings_uac1[] = {
[STR_ASSOC].s = "Source/Sink",
[STR_AC_IF].s = "AC Interface",
[STR_USB_OUT_IT].s = "Playback Input terminal",
[STR_USB_OUT_IT_CH_NAMES].s = "Playback Channels",
[STR_IO_OUT_OT].s = "Playback Output terminal",
[STR_IO_IN_IT].s = "Capture Input terminal",
[STR_IO_IN_IT_CH_NAMES].s = "Capture Channels",
[STR_USB_IN_OT].s = "Capture Output terminal",
[STR_AS_OUT_IF_ALT0].s = "Playback Inactive",
[STR_AS_OUT_IF_ALT1].s = "Playback Active",
[STR_AS_IN_IF_ALT0].s = "Capture Inactive",
[STR_AS_IN_IF_ALT1].s = "Capture Active",
{ },
};
int main()
{
char buf[] = "qwerrrr";
strings_uac1[STR_ASSOC].s = (char*)buf;
//strcpy(strings_uac1[STR_ASSOC].s,"helloworld");
/* Write C code in this online editor and run it. */
printf("Hello, World! %s\n",strings_uac1[STR_ASSOC].s);
return 0;
}
#include <stdio.h>
#include <string.h>
struct usb_string {
char id;
char * const s;
};
enum {
STR_ASSOC,
STR_AC_IF,
STR_USB_OUT_IT,
STR_USB_OUT_IT_CH_NAMES,
STR_IO_OUT_OT,
STR_IO_IN_IT,
STR_IO_IN_IT_CH_NAMES,
STR_USB_IN_OT,
STR_AS_OUT_IF_ALT0,
STR_AS_OUT_IF_ALT1,
STR_AS_IN_IF_ALT0,
STR_AS_IN_IF_ALT1,
};
static struct usb_string strings_uac1[] = {
[STR_ASSOC].s = "Source/Sink",
[STR_AC_IF].s = "AC Interface",
[STR_USB_OUT_IT].s = "Playback Input terminal",
[STR_USB_OUT_IT_CH_NAMES].s = "Playback Channels",
[STR_IO_OUT_OT].s = "Playback Output terminal",
[STR_IO_IN_IT].s = "Capture Input terminal",
[STR_IO_IN_IT_CH_NAMES].s = "Capture Channels",
[STR_USB_IN_OT].s = "Capture Output terminal",
[STR_AS_OUT_IF_ALT0].s = "Playback Inactive",
[STR_AS_OUT_IF_ALT1].s = "Playback Active",
[STR_AS_IN_IF_ALT0].s = "Capture Inactive",
[STR_AS_IN_IF_ALT1].s = "Capture Active",
{ },
};
int main()
{
char buf[] = "qwerrrr";
strings_uac1[STR_ASSOC].s = (char*)buf;
//strcpy(strings_uac1[STR_ASSOC].s,"helloworld");
/* Write C code in this online editor and run it. */
printf("Hello, World! %s\n",strings_uac1[STR_ASSOC].s);
return 0;
}
#include <stdio.h>
#include <string.h>
struct usb_string {
char id;
char * const s;
};
enum {
STR_ASSOC,
STR_AC_IF,
STR_USB_OUT_IT,
STR_USB_OUT_IT_CH_NAMES,
STR_IO_OUT_OT,
STR_IO_IN_IT,
STR_IO_IN_IT_CH_NAMES,
STR_USB_IN_OT,
STR_AS_OUT_IF_ALT0,
STR_AS_OUT_IF_ALT1,
STR_AS_IN_IF_ALT0,
STR_AS_IN_IF_ALT1,
};
static struct usb_string strings_uac1[] = {
[STR_ASSOC].s = "Source/Sink",
[STR_AC_IF].s = "AC Interface",
[STR_USB_OUT_IT].s = "Playback Input terminal",
[STR_USB_OUT_IT_CH_NAMES].s = "Playback Channels",
[STR_IO_OUT_OT].s = "Playback Output terminal",
[STR_IO_IN_IT].s = "Capture Input terminal",
[STR_IO_IN_IT_CH_NAMES].s = "Capture Channels",
[STR_USB_IN_OT].s = "Capture Output terminal",
[STR_AS_OUT_IF_ALT0].s = "Playback Inactive",
[STR_AS_OUT_IF_ALT1].s = "Playback Active",
[STR_AS_IN_IF_ALT0].s = "Capture Inactive",
[STR_AS_IN_IF_ALT1].s = "Capture Active",
{ },
};
int main()
{
char buf[] = "qwerrrr";
//strings_uac1[STR_ASSOC].s = (char*)buf;
strncpy(strings_uac1[STR_ASSOC].s,"hellorld",5);
/* Write C code in this online editor and run it. */
printf("Hello, World! %s\n",strings_uac1[STR_ASSOC].s);
return 0;
}