LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 07-11-2003, 09:20 PM   #1
surya123
LQ Newbie
 
Registered: Jul 2003
Location: Bali-Indonesia
Distribution: local
Posts: 11

Rep: Reputation: 0
Lightbulb about STRING in gcc


to continue about string, pls look to this:
Code:
01#include <stdio.h>
02#include <string.h>
03
04#define STR_SIZE 70
05
06struct reckey
07{
08  char *name;
09};
11typedef struct reckey key;
12
13int main()
14{
15  char *S,*D,*T,*P,*K,A[STR_SIZE];
16  key *user;
17
18  S = "123456789 223456789 323456789 423456789 523456789 623456789\n"; 
19  //if we insert string to S with over than 32 karakter it will make strcpy(P,S) causing segment fault.
20
21  user = (key *)malloc(sizeof(key)); 
22  user->name = (char *)malloc(sizeof(char)*STR_SIZE);
23  strcpy(user->name,S);
24
25  D = S;
26  printf("%s",D);
27
28  strcpy(A,S);
29  printf("%s",A);
30
31  T = A;
32  printf("%s",T);
33
34  P = (char *)malloc(sizeof(char));  
35  strcpy(P,A);
36  printf("%s",P);
37
38  K = &A[0];  //equal with K = A;
39  printf("K=%s A=%s",K,A);
40
41  printf("user->name %s",user->name);
}
i compiled with: "gcc string.c"
run : "./a.out"
output:

123456789 223456789 323456789 423456789 523456789 623456789
123456789 223456789 323456789 423456789 523456789 623456789
123456789 223456789 323456789 423456789 523456789 623456789
123456789 223456789 323456789 423456789 523456789 623456789
K=123456789 223456789 323456789 423456789 523456789 623456789
A=123456789 223456789 323456789 423456789 523456789 623456789
user->name 123456789 223456789 323456789 423456789 523456789 623456789

why syntax strcpy() can do its job when i didnt type "#include <string.h>" in line-02?
in line-22 and line-34, if i didnt include multiply STR_SIZE, the result is ok, anyone know why? or when P = (char *)malloc(sizeof(char)) how many karakter that it preserved exactly?
For this P = (char *)malloc(sizeof(char)*10) its explanation by Dark_Helmet that i've read is very clear.thx.

my conclusions:
if we want using strcpy then the syntax is: strcpy(array,string)
examples:
Code:
char *String1, *String2, Array[STR_SIZE];
key *user;

   //to fill words/sentence from string_variable to other string_variable:

   String1 = String2; Ok
   strcpy(Array1,String1); Ok
   String1 = Array1; Ok
   String1 = Array1[0]; Ok
   String1 = (char *)malloc(sizeof(char));
   strcpy(String1,String2); Ok

   user = (key *)malloc(sizeof(key)); 
   user->name = S; Not Ok! even the result is Ok but in LIFO will cause a problem. 		(see in threads "what happen to my LIFO" and the explanation by Dark_Helmet)

   user = (key *)malloc(sizeof(key)); 
   user->name = (char *)malloc(sizeof(char)*STR_SIZE);
   strcpy(user->name,S); Ok
if anyone has opinion to my conclusions, please tell me, i need the truth understanding. i'm very grateful.
 
  


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
in gcc compiler error: parse error before string constsnt cynthia_thomas Linux - Networking 1 10-20-2005 01:29 AM
Bash way to tell if String is in String tongar Programming 3 06-16-2005 06:59 AM
C....Search a string for a string Scrag Programming 4 06-14-2004 04:15 PM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM
GCC - including <string.h> (won't work) mymojo Linux - Newbie 3 10-17-2003 05:58 PM

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

All times are GMT -5. The time now is 04:44 PM.

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