-
char problem in C
i have a char array: char x[20];
i did the following: strcpy(x, argv[2]);
but i want to add one space in the begining and end of the char array for example say argv[2] is "abc" so i want x to contain " abc " how can i do this?
thx
Slackware-current with kernel 2.6.4
-
this should pretty much do it
Code:
char x[20];
strcpy(x, " ");
strcat(x, argv[2]);
strcat(x, " ");
------------------------------------------------------------------
"Dimensions will always be expressed in the least usable term, convertible only through the use of weird and unnatural conversion factors. Velocity, for example, will be expressed in furlongs per fortnight."
-
Thx very much i could not think of it anyway i am new to C programming....
Slackware-current with kernel 2.6.4
-
Anytime. We're here to help.
------------------------------------------------------------------
"Dimensions will always be expressed in the least usable term, convertible only through the use of weird and unnatural conversion factors. Velocity, for example, will be expressed in furlongs per fortnight."
-
another way to do that:
Code:
int x[20];
sprintf(x," %s ",argv[2]);
-
or
Code:
char x[20];
sprintf(x," %.17s ",argv[2]);
That should ensure the string will actually fit within the allocated space.
My Machine:
Maytag SAV5905
710 rpm Stainless Steel Drum
Dual boot: Gentoo / Tide
-
Wow prety much C coders here...i want to be a good C coder too thx for different technics..By the way another question i have in the forum about exec()....
take a look
Slackware-current with kernel 2.6.4
-
Or
Code:
char x[20];
snprintf(x, 20, " %s ", argv[2]);
(another way to not overflow the buffer).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|