LinuxQuestions.org
Help answer threads with 0 replies.
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-01-2010, 03:26 AM   #1
utkarshrawat
Member
 
Registered: Jul 2007
Posts: 120

Rep: Reputation: 15
strcpy problem


I am getting Segmentation fault error .Dont know where it is
Code:
#include <unistd.h>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
main()
{
	uint8_t ip[4];
	strcpy(ip[0],"192");	
	printf("%s",ip[0]);
}
Getting Segmentation fault
 
Old 07-01-2010, 03:32 AM   #2
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
The changes have been marked in red.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
main()
{
  uint8_t ip[4];
  strcpy(ip, "192");
  printf("%c",ip[0]);
}
 
Old 07-01-2010, 03:34 AM   #3
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Do read the following link completely, PLEASE
http://www.unknownroad.com/rtfm/gdbtut/gdbtoc.html
 
Old 07-01-2010, 03:36 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well upon a quick search on google, most links seem to say that the definition for uint8_t come from stdint.h which you don't seem to have included.
Not a 100% sure but this would be a good place to start.
 
Old 07-01-2010, 03:58 AM   #5
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
At least read this one:
http://www.unknownroad.com/rtfm/gdbtut/gdbsegfault.html
 
Old 07-01-2010, 05:44 AM   #6
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
The changes anishakaul suggested should fix the seg fault that the original code hits as well as the next seg fault it would hit if just the first were corrected. But the second correction probably doesn't fit the original intention of the code. I think the second correction should be the same as the first:
Code:
	printf("%s",ip);
ip (when passed as a parameter to a function) is the address of element zero of the array ip[]. The address of element zero is the same address and in this example the same type as the address of the array itself.

ip[0] is the value of element zero.

Those two function calls each need an address for the parameter for which the OP passed ip[0].
 
1 members found this post helpful.
Old 07-01-2010, 05:52 AM   #7
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by johnsfine View Post
I think the second correction should be the same as the first:
Code:
	printf("%s",ip);
You are right, i tested it just now.
 
Old 07-01-2010, 06:14 AM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Such problems are immediately caught at compilation stage by '-Wall- Wextra -Wformat=2'.
 
Old 07-02-2010, 07:56 AM   #9
utkarshrawat
Member
 
Registered: Jul 2007
Posts: 120

Original Poster
Rep: Reputation: 15
IP should be stored in ip[3]

Thanks to all for the reply .It came
Now I want an IP(Internet Protocol) should be stored in uint_t ip[3];
ip[0]=192;
ip[1]=168;
ip[2]=1;
ip[3]=123;

output should come like this 192.168.1.123,with the (.)dot
 
Old 07-02-2010, 08:00 AM   #10
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by utkarshrawat View Post
Now I want an IP(Internet Protocol) should be stored in uint_t ip[3];
ip[0]=192;
ip[1]=168;
ip[2]=1;
ip[3]=123;

output should come like this 192.168.1.123,with the (.)dot
You better make that uint_t ip[4] because declaring 3 only lets you use 0..2.

Beyond that, you seem to be asking us to write your code for you.

If this is homework, you shouldn't ask that and others replying shouldn't do it.

Even if it isn't homework, please make some real effort at doing your own work.
 
Old 07-02-2010, 01:46 PM   #11
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
uint_t ip[4][4] ?
 
Old 07-05-2010, 11:14 AM   #12
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
int ip[4];

AFAIK he wants the individual elements to be integers, not strings.
 
  


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
strcpy in java ? Alexander.s Programming 4 08-30-2008 04:03 PM
diff between strcpy and strncpy djgerbavore Programming 11 08-04-2004 07:01 AM
strcpy problem rajatgarg Programming 5 11-20-2003 12:46 AM
question with strcpy Jo_Nak Programming 1 07-02-2003 04:23 PM
strcpy to an array of strucs gyroWang Programming 4 03-22-2003 10:01 PM

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

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