LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-07-2005, 07:30 AM   #1
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Rep: Reputation: 45
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
 
Old 09-07-2005, 07:45 AM   #2
AltF4
Member
 
Registered: Sep 2002
Location: .at
Distribution: SuSE, Knoppix
Posts: 532

Rep: Reputation: 31
>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 :-)
 
Old 09-07-2005, 08:28 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

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

char * = char[]
 
Old 09-07-2005, 09:15 AM   #4
lowpro2k3
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Rep: Reputation: 30
..
 
Old 09-07-2005, 12:08 PM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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";
 
Old 09-07-2005, 12:20 PM   #6
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Original Poster
Rep: Reputation: 45
Unhappy 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
 
Old 09-07-2005, 12:40 PM   #7
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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.
 
Old 09-07-2005, 02:41 PM   #8
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Original Poster
Rep: Reputation: 45
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 ...
 
Old 09-07-2005, 03:13 PM   #9
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
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?
 
Old 09-07-2005, 03:43 PM   #10
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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.
 
Old 09-07-2005, 07:17 PM   #11
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Original Poster
Rep: Reputation: 45
Thx a lot can u still correct my little programme? Where have u learned all this things?
 
Old 09-07-2005, 07:34 PM   #12
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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?
 
Old 09-07-2005, 07:40 PM   #13
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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.
 
Old 09-07-2005, 07:53 PM   #14
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Original Poster
Rep: Reputation: 45
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?
 
Old 09-07-2005, 08:10 PM   #15
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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

Last edited by paulsm4; 09-07-2005 at 08:12 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C pointers confusion - char ** = char [][] ?? saravkrish Programming 12 12-02-2004 10:06 AM
C Problem---convert char to char* totti10 Programming 11 11-06-2004 11:32 AM
invalid conversion from `char' to `const char* bru Programming 6 05-09-2004 03:07 PM
convert from char* to unsigned char* D J Linux - Software 2 02-20-2004 04:09 AM
Copy a char* to another char* sathyan Programming 8 07-24-2003 06:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration