LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   char * declaration in c; (https://www.linuxquestions.org/questions/programming-9/char-%2A-declaration-in-c%3B-361006/)

alaios 09-07-2005 07:30 AM

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

AltF4 09-07-2005 07:45 AM

>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 :-)

bigearsbilly 09-07-2005 08:28 AM

char * is an array of characters. Typically used as a string.
char ** is an array of the above "strings"

char * = char[]

lowpro2k3 09-07-2005 09:15 AM

..

jlliagre 09-07-2005 12:08 PM

Quote:

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?
columns need to be allocated:
Code:

char **columns;
columns=(char**)malloc(2*sizeof(char*));
columns[0]="rate";
columns[1]="burst";


alaios 09-07-2005 12:20 PM

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

jlliagre 09-07-2005 12:40 PM

Quote:

char *text;
text="kdakka";
This sometime works because the compiler allocates memory just for stroring the string.. Is this right?
Wrong, this always works.
The string is the one you entered in your code, there is no malloc or anything like it.

Quote:

What are the size limitations?
Your editor will probably put limits before the compiler itself.

Quote:

Perphaps the maximum size is somewhere defined
perhaps.

Quote:

You have suggest me to allocate some memory with this statement
columns=(char **)malloc(2*sizeof(char));

How much memory does this thing allocate?
No, I wrote 2* sizeof(char*)

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.

alaios 09-07-2005 02:41 PM

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 ...

Hivemind 09-07-2005 03:13 PM

Code:

const char *foo = "foobar";
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?

jlliagre 09-07-2005 03:43 PM

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.

alaios 09-07-2005 07:17 PM

Thx a lot can u still correct my little programme? Where have u learned all this things?

paulsm4 09-07-2005 07:34 PM

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?

jlliagre 09-07-2005 07:40 PM

Quote:

can u still correct my little programme?
This looks odd:
Code:

printf("Colums0 einai %d",columns[i]);
columns[i] is not a number but you are printing it as one.
Quote:

Where have u learned all this things?
I started with Kernighan and Ritchie.

alaios 09-07-2005 07:53 PM

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?

paulsm4 09-07-2005 08:10 PM

Code:

query=malloc(1000);
query=strcat(query,"SELECT ");

<= This is a bug. It *might* work 99 times out of 100, but you haven't initialized the string "query" before you you call "strcat()" with it:
Code:

CORRECTION:
query = malloc (1000);
query[0] = '\0';
query = strcat(query, "SELECT ");

I believe this code is even closer to what you actually want to do:
Code:

CORRECTION 2:
#include <stdio.h>
#include <malloc.h>
#include <errno.h>
...
#define BUFSIZE 255
...

  buff = malloc(BUFSIZE);
  if (buff == NULL)
  {
    printf ("ERROR: Unable to allocate %d bytes, errno: %d!\n",
      BUFSIZE, errno);
    return ERROR_STATUS;
  }
  strncpy (buff, "SELECT ", BUFSIZE);
  ...

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).

Please buy K & R. At your earliest opportunity!

Your .. PSM


All times are GMT -5. The time now is 05:38 PM.