char * declaration in c;
Hi i have a question... When i declare a char *query; in c how much size is allocated for the variable... I think that this simple defines a pointer to a char character and nothing more... If that is true
why the following doesnt generate an error? char *query; query="select"; Where the select string is stored if i have declared only a pointer? Something else... I am trying to pass a number of arguments to a function.. the parameters are not a constant number and can change from time to time.. I have thought that the same case exists in main function int argc,char **argv Firstly i would like to ask whats the differece betwenn *argv and **argv i have noticed that if i declare a variable with char **columns; then the two following lines are valid columns[0]="hi"; columns[1]="How are u?" At the beggining i have thought that the declartaion columns[1] will overlap the string of columns[0] After checkign it i have noticed that this dont happen... Finally i am trying to pass an array of variables to a function so i have declared it like that char *query(int num_columns,char **columns,char *table){ char *query; *query="select"; for (i=0;i<num_columns;i++) sprintf(query,"%s",columns[i]); /*Here occurs segmentation fault */ } In the main function now char **columns; columns[0]="rate"; columns[1]="burst"; printf("%s \n",query(2,columns,"htb")); /*Call the query function */ This code ends with segmentation fault at the query function... What do u have to suggest me?Thx a lot |
>char *query;
>query="select"; > >Where the select string is stored if i have declared only a pointer? the string "select\0" is stored somewhere in your compiled code. depending on your OS/computer architecture it will likely be in a data segment . > I am trying to pass a number of arguments to a function. try "man stdarg" NAME stdarg - variable argument lists SYNOPSIS #include <stdarg.h> void va_start(va_list ap, last); type va_arg(va_list ap, type); void va_end(va_list ap); void va_copy(va_list dest, va_list src); DESCRIPTION A function may be called with a varying number of argu_ ments of varying types. The include file stdarg.h declares a type va_list and defines three macros for step_ ping through a list of arguments whose number and types are not known to the called function. > Firstly i would like to ask whats the differece betwenn *argv and **argv char *argv ... pointer to a character (i.e. the first element in an array of chars) char **argv ... pointer to "char *" (i.e. the first element in an array of "char *") >char *query="select"; >for (i=0;i<num_columns;i++) >sprintf(query,"%s",columns[i]); /*Here occurs segmentation fault */ this reserves 7 characters ("select" + terminating '\0') and tries to fill longer strings into the reserved space --> coredump you need to reserve a larger array ( char buffer[2048] ), then you can fill in 2K of text :-) |
char * is an array of characters. Typically used as a string.
char ** is an array of the above "strings" char * = char[] |
..
|
Quote:
Code:
char **columns; |
i am confused
Thx a lot but i am confused... let me plz start asking
char *text; text="kdakka"; This sometime works because the compiler allocates memory just for stroring the string.. Is this right? What are the size limitations? Perphaps the maximum size is somewhere defined You have suggest me to allocate some memory with this statement columns=(char **)malloc(2*sizeof(char)); How much memory does this thing allocate? Check my programme source code and at execution time char *query(int num_columns,char **columns,char *table){ int i=0; char *query; query="SELECT"; printf("print to Seg \n"); printf("Colums0 einai %d",columns[i]); for (i=0;i<num_columns;i++) sprintf(query,"%s",columns[i]); printf("Meta to loop \n"); return query; } int mysqltest(void *test){ MYSQL_RES *res; MYSQL_ROW row; mysql_init(diff_db); char **columns; columns=(char **)malloc(2*sizeof(char)); columns[0]="rate"; columns[1]="burst"; printf("%s \n",query(2,columns,"htb")); return 0; } ./executeit print to Seg Colums0 einai 134523268Segmentation fault it prints some value that are strange 1345232... and ends with a seg fault |
Quote:
The string is the one you entered in your code, there is no malloc or anything like it. Quote:
Quote:
Quote:
This is the size your system needs to store two character pointers. The actual size (likely 64 bits or 128 bits) will depend on your architecture. |
I cant understand something...
When i declare int myvariable; The compiler know that need to reserve 8 bytes (or 16 bytes) for storing the future values Why this is not necessary for a char... I can understand the char *test that declares a pointer that points to characters.. but still cant understand that the test="100000000 characters " where is stored ... |
Code:
const char *foo = "foobar"; |
Quote:
Quote:
Quote:
Quote:
Try compiling with the "-S" option, and look at the assembly code, you'll see your long string there. |
Thx a lot can u still correct my little programme? Where have u learned all this things?
|
Hi, Alois -
Have you bought the copy of Kernighan and Ritchie we discussed the other day yet? How are things coming with your SNMP project? |
Quote:
Code:
printf("Colums0 einai %d",columns[i]); Quote:
|
strange thins..
I have change my code a little but still some things make me feel so strange
query=malloc(1000); query=strcat(query,"SELECT "); //query="SELECT"; printf("print to Seg \n"); printf("Colums0 einai %s",columns[i]); for (i=0;i<num_columns;i++){ query=strcat(query,columns[i]); This thing works but if i uncomment the //query="SELECT"; then this ends with a segfault when the query=strcat(query,.....) Can u explaine me why this happens? |
Code:
query=malloc(1000); Code:
CORRECTION: Code:
CORRECTION 2: If you want read/write memory, you need to allocate it yourself. With "malloc()" (off the heap), or with a local declaration (off the stack). Please buy K & R. At your earliest opportunity! Your .. PSM |
All times are GMT -5. The time now is 05:38 PM. |