ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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 :-)
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);
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 ...
In this definition "foobar" is a string literal. string literals have static storage, meaning they exist throughout the lifetime of the program. Where are they stored? Wherever the compiler decides, it's not dictated by the standard afaik. Does that answer your question?
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,788
Rep:
Quote:
When i declare int myvariable;
The compiler know that need to reserve 8 bytes (or 16 bytes) for storing the future values
Not really, an int is usually 4 bytes long.
Quote:
Why this is not necessary for a char
It is exactly the same for a char, the compiler reserve one byte, and 4 or 8 bytes for a char pointer.
Quote:
... I can understand the char *test that declares a pointer that points to characters..
That's good :-)
Quote:
but still cant understand that the test="100000000 characters " where is stored ...
it is stored in your binary program, not allocated at run time.
Try compiling with the "-S" option, and look at the assembly code, you'll see your long string there.
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?
As far as that "query = "SELECT " - don't even go there. Basically, you're assigning a pointer (query) to a string that's in read-only memory. A crash will assuredly occur sooner (when it's easier to debug) or later (when it's much, much harder to debug).
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).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.