LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-09-2017, 04:33 AM   #1
inmove
LQ Newbie
 
Registered: Feb 2016
Posts: 3

Rep: Reputation: Disabled
same type, same address, but different value?


the code:

Code:
char *sendbuf = (char *)malloc(sizeof(char) * 1024);
char *sendbuftmp = sendbuf;
sendbuf = "hello world\0";                                                                                                                      

printf("sendbuftmp: %s, %d\n", sendbuftmp, strlen(sendbuftmp));
printf("sendbuf: %s, %d\n", sendbuf, strlen(sendbuf));
the output is:

sendbuftmp: , 0
sendbuf: hello world, 11

------------------------------------------------------
sendbuf, sendbuftmp have the same address and the same type, but them have the different value, it confused me seriously.can anyone explain why, thx.
 
Old 10-09-2017, 04:44 AM   #2
inmove
LQ Newbie
 
Registered: Feb 2016
Posts: 3

Original Poster
Rep: Reputation: Disabled
char *sendbuf = (char *)malloc(sizeof(char) * 1024);
sendbuf = "hello world\0";
char *sendbuftmp = sendbuf;

printf("sendbuftmp: %s, %d\n", sendbuftmp, strlen(sendbuftmp));
printf("sendbuf: %s, %d\n", sendbuf, strlen(sendbuf));

if i exchange the third and second line, the output is:

hello world,11
hello world,11

as expected.
 
Old 10-09-2017, 06:34 AM   #3
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,622

Rep: Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610
"hello world" is a string, it has its own address.
Code:
sendbuf = "hello world\0";
means sendbuf (which is a pointer) will get a new value, will point to the H of hello. The old value will be lost.

Code:
char *sendbuf = (char *)malloc(sizeof(char) * 1024);
printf("sendbuf: %s, %d\n", sendbuf, strlen(sendbuf));
is incorrect, because the 1024 chars were not initialized, so stren has no any meaning.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Specifying address type of a packet Faiza Al-Salti Linux - Networking 5 07-21-2016 07:40 AM
[SOLVED] when i type in ifconfig it doesn`t show me my IP address Changedirectory Linux - Newbie 6 03-26-2016 04:13 AM
How to find ISP connection type from IP address vignesh4sh Linux - Server 5 06-27-2013 10:37 AM
[SOLVED] when using hotmail i can type in the email address but the curser will not appear i ponty Linux - Newbie 2 06-16-2010 05:17 AM
How to know IPv6 address-type ??? neo3matrix Solaris / OpenSolaris 2 01-20-2010 10:56 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:35 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