Concatenate two strings


 
Thread Tools Search this Thread
Top Forums Programming Concatenate two strings
# 1  
Old 09-18-2013
Concatenate two strings

Hello!
Can anyone explain line 28 for me? I was thinking *a would be replaced by *b, but it actually appends *a to *b. I know it is related to pointer address, but could not figure it out by myself. Thanks a lot!
Code:
  1 //Concatenate two strings
  2
  3 #include<stdio.h>
  4 char *stradd (char *, char *);
  5
  6 int main()
  7 {
  8   char a[] = "abcdefg";
  9   char b[] = "GHIJKLM";
 10   char *c;
 11   printf ("a: %s\n", a);
 12   printf ("b: %s\n", b);
 13   c = stradd (a, b);
 14   printf("c: %s\n", c);
 15 }
 16
 17 char *stradd (char *a, char *b)
 18 {
 19   char *p;
 20   p = a;
 21   while (*a != '\0')
 22     {
 23       a++;
 24       printf("%p\n", a);
 25     }
 26   while (*b != '\0')
 27     {
 28       *a = *b;
 29      // printf("+++%p\n", a);
 30       b++;
 31      // printf("%p\n", b);
 32       a++;
 33      // printf("append-%p\n", a);
 34     }
 35   return p;
 36
 37 }

# 2  
Old 09-18-2013
First off, you're writing beyond the bounds of the array, which is a bad thing. You should give a[] room to have stuff added.

Code:
char a[256] = "abcdefg";
char b[256] = "GHIJKLM";

Second off, look what the function does:

Code:
char *stradd (char *a, char *b)
{
        char *p;
        p = a;

        // Before the loop, A points to the beginning of the string.

        while (*a != '\0') // Loop until you hit the NULL terminator
        {
                a++;
                printf("%p\n", a);
        }

        // A now points to the end of the string.  Writing to the end
        // will add to the end.

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 09-18-2013
Thanks!
Got the part for a[256], but not the stradd() function. I thought to use a = b, why shouldn't I?
I should have used *ptr1 and *ptr2 for the stradd() function to avoid confusion.
# 4  
Old 09-18-2013
Quote:
Originally Posted by yifangt
Thanks!
Got the part for a[256], but not the stradd() function. I thought to use a = b, why shouldn't I?
Consider it like this:
Code:
char arraya[64]="abcdefg";
char arrayb[64]="qwerty";

int a=0;
int b=5;

printf("%c\n", arraya[a]);
printf("%c\n", arrayb[b]);

// Altering a does not alter arraya, just a!
a=b;

'char *' is like that variable 'a' up there. It doesn't tell you anything but where memory is.

The * operator dereferences it, (*a) in your code is like arraya[a] in my example.

The [] operator also dereferences it, like a[2] in your code would be like arraya[a+2] in my example.

Now, imagine that all memory in your process is all one great big array, so that 'a' and 'b' are simply different locations in the same big block. Altering those numbers doesn't change the memory they refer to.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 09-18-2013
Definitely I did not catch the basic work of pointer in C. I am falling into the same pitfall of C most people did, but I want get out as soon as possible. Needs more reading.
Thanks a lot!
# 6  
Old 09-18-2013
This has helped me with pointers and pointers arithmetic: http://www.eskimo.com/~scs/cclass/notes/sx10.html but i can't say i have mastered it yet, au contraire. You probably know that there is a stdlib function called "strcat" ?
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Concatenate strings in a a for loop

hi guys, I have this question. I am creating an script to that read a text file(.ini) with the list of the patterns to find for example: EPMS_VO EMPS_PARTS Then it check if file have been delivered in a folder and process it with each pattern, but I am having problems concatenting the... (7 Replies)
Discussion started by: Danman
7 Replies

2. Shell Programming and Scripting

Concatenate strings

Hi,I'm trying to concatenate @example.com to each line of a file f1.txt. and push it into f2.txt. Here is the code i'm using. for i in `cat /home/linux1/xxxxxxx/f1.txt`; do echo ${i}@example.com > /home/linux1/xxxxxx/f2.txt; done But above code only printing @example.com in f2.txt. what... (18 Replies)
Discussion started by: sam_bd
18 Replies

3. Shell Programming and Scripting

Concatenate text between patterns in individual strings

In any given file, wherever a certain data block exists I need to concatenate the values(text after each "=" sign) from that block. in that block. The block starts and ends with specific pattern, say BEGIN DS and END DS respectively. The block size may vary. A file will have multiple such blocks.... (12 Replies)
Discussion started by: Prev
12 Replies

4. Web Development

Concatenate Strings

hi..:) this is my sample part of my program.. $csv_output .= $row.",". $row.",". $row.",". $row.",". $row.",". ... (2 Replies)
Discussion started by: Jeneca
2 Replies

5. UNIX for Dummies Questions & Answers

concatenate strings

if i use echo "ravi" echo "sankar" it showing output ravi sankar but i want output as ravi sankar remember sankar should be in another echo statement only (2 Replies)
Discussion started by: shankr3
2 Replies

6. Shell Programming and Scripting

delete repeated strings (tags) in a line and concatenate corresponding words

Hello friends! Each line of my input file has this format: word<TAB>tag1<blankspace>lemma<TAB>tag2<blankspace>lemma ... <TAB>tag3<blankspace>lemma Of this file I need to eliminate all the repeated tags (of the same word) in a line, as in the example here below, but conserving both (all) the... (2 Replies)
Discussion started by: mjomba
2 Replies

7. UNIX for Dummies Questions & Answers

Concatenate Strings

Hi Friends, I have a requirement I need to concatenate the below two strings. String 1 = /@jobid_at_ String 2 = value stored in ORACLE_SID String 3 = string1 concatenated with String 2. Please let me know how should i do it in UNIX. Thanks, (2 Replies)
Discussion started by: diva_thilak
2 Replies

8. Shell Programming and Scripting

How to concatenate two strings or several strings into one string in B-shell?

like connect "summer" and "winter" to "summerwinter"? Can anybody help me? thanks a lot. (2 Replies)
Discussion started by: fontana
2 Replies
Login or Register to Ask a Question