Arrays issue

A

ataanis

Hi,
I have the following statement, and i'm trying to understand why when
I do the second print it's giving me segmentation fault, both name
and s_aliases are char arrays e.g
THE FIRST PRINT WORKS FINE , which means that the aliases are in
s_aliases

char names[NAMESIZE] and char s_aliases[2], h is a structure , that
contains s_aliases
defined as
struct servent *h and same for hostsev


while (h -> s_aliases != NULL)
{
printf("Alias %d : \t%s\n",i, h->s_aliases);
hostsev.names = *h -> s_aliases;
i++;
} // loop thru aliases


printf("the first element in the hostsev aliases is %s \n",
hostsev.names[0]);
Can anybody tell me why?
 
S

santosh

Hi,
I have the following statement, and i'm trying to understand why when
I do the second print it's giving me segmentation fault, both name
and s_aliases are char arrays e.g
THE FIRST PRINT WORKS FINE , which means that the aliases are in
s_aliases

char names[NAMESIZE] and char s_aliases[2], h is a structure , that
contains s_aliases
defined as
struct servent *h and same for hostsev


If you supply the actual declaration of your structures, then we can
provide better answers. Copy and paste them from the source code -
don't retype.
while (h -> s_aliases != NULL)
{
printf("Alias %d : \t%s\n",i, h->s_aliases);
hostsev.names = *h -> s_aliases;


You cant simply assign on string to another like this. You'll have to
copy them character-by-character. Further I suspect the way your
accessing h->s_aliases in the above expression is wrong.
 
A

ataanis

Santosh ,

struct service {
int sequence;
int port;
char names[NAMESIZE];
} hostsev;

struct service {
int sequence;
int port;
char names[NAMESIZE];
} hostsev;

struct servent *h;

while (h -> s_aliases != NULL)
{
printf("Alias %d : \t%s\n",i, h->s_aliases);
hostsev.names = * h->s_aliases;
printf("the content of the structure %s \n",
hostsev.names);
i++;
} // loop thru aliases

The first printf works , and the second doesn't which I believe means
that it's not storing
Thanks
 
R

Richard Heathfield

(e-mail address removed) said:

struct servent *h;

while (h -> s_aliases != NULL)


Stop right there. h is a pointer, but you haven't pointed it at any struct
servent object. Therefore its value is indeterminate, and any attempt to
dereference that value invokes undefined behaviour.

Point it somewhere useful.

<snip>
 
F

Flash Gordon

Hi,
I have the following statement, and i'm trying to understand why when
I do the second print it's giving me segmentation fault, both name
and s_aliases are char arrays e.g
THE FIRST PRINT WORKS FINE , which means that the aliases are in
s_aliases

char names[NAMESIZE] and char s_aliases[2], h is a structure , that
contains s_aliases
defined as
struct servent *h and same for hostsev


Why go to all this effort to provide a hard to read description when you
could have simply posted some code?
while (h -> s_aliases != NULL)
{
printf("Alias %d : \t%s\n",i, h->s_aliases);
hostsev.names = *h -> s_aliases;
i++;
} // loop thru aliases


printf("the first element in the hostsev aliases is %s \n",
hostsev.names[0]);
Can anybody tell me why?


Yes, you've got an error in your code. Try posting a small complete
compilable example of standard C code that shows your problem and
someone might be able to be more specific.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc

Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
 
A

ataanis

Santosh , the message was supposed to be

struct service {
int sequence;
int port;
char names[NAMESIZE];
} hostsev;


struct servent {
char *s_name; /* official service name
*/
char **s_aliases; /* alias list */
int s_port; /* port number */
char *s_proto; /* protocol to use */
}


struct servent *h;


while (h -> s_aliases != NULL)
{
printf("Alias %d : \t%s\n",i,
h->s_aliases);
hostsev.names = * h->s_aliases;

printf("the content of the structure %s
\n",
hostsev.names);
i++;
} // loop thru aliases


The first printf works , and the second doesn't which I believe means
that it's not storing
Thanks
 
S

santosh

Santosh , the message was supposed to be

struct service {
int sequence;
int port;
char names[NAMESIZE];
} hostsev;


struct servent {
char *s_name; /* official service name
*/
char **s_aliases; /* alias list */
int s_port; /* port number */
char *s_proto; /* protocol to use */
}


struct servent *h;


while (h -> s_aliases != NULL)


As Richard says, you haven't set h to point to any instance of servant
structure.
{
printf("Alias %d : \t%s\n",i,
h->s_aliases);
hostsev.names = * h->s_aliases;


Are you sure that NAMESIZE will be sufficient here? If not then data
might get overwritten. Also you've to do a element wise copy here.
printf("the content of the structure %s
\n",
hostsev.names);
i++;
} // loop thru aliases


The first printf works , and the second doesn't which I believe means
that it's not storing


Yes, you're not copying the strings at all. You need another loop to do
that. Something like:

for(cnt = 0; h->s_aliases[cnt] != '\0'; ++cnt)
hostsev.names[cnt] = h->s_aliases[cnt];
 
A

ataanis

I respect your answer, but if that's the case then how come I get the
following output ?

Alias 0 : postoffice
Alias 1 : pop-2
Segmentation fault
That means that the first print is working and the second is not
Thanks
 
A

ataanis

Santosh , the message was supposed to be

struct service {
int sequence;
int port;
char names[NAMESIZE];
} hostsev;


struct servent {
char *s_name; /* official service name
*/
char **s_aliases; /* alias list */
int s_port; /* port number */
char *s_proto; /* protocol to use */
}


struct servent *h;


while (h -> s_aliases != NULL)
{
printf("Alias %d : \t%s\n",i,
h->s_aliases);
hostsev.names = * h->s_aliases;



printf("the content of the structure %s

\n",
hostsev.names);
i++;
} // loop thru aliases


The first printf works , and the second doesn't which I believe means
that it's not storing
Thanks
 
R

Richard Heathfield

(e-mail address removed) said:
I respect your answer, but if that's the case then how come I get the
following output ?

Alias 0 : postoffice
Alias 1 : pop-2
Segmentation fault
That means that the first print is working and the second is not

I jumped off a cliff with no safety net, and hit a few rocks on the way
down. I didn't sustain any serious injuries on hitting the first rock, but
on the second I broke my leg, so obviously jumping off wasn't a mistake - I
must have done something wrong after the first bounce.
 
A

ataanis

Richard,
ok i'm really sorry but I believe this is the statement I forgot to
mention in my previous posts.

h = getservbyname(name,protocol)
 
R

Richard Heathfield

(e-mail address removed) said:
Richard,
ok i'm really sorry but I believe this is the statement I forgot to
mention in my previous posts.

h = getservbyname(name,protocol)

One cannot help but wonder what other highly relevant information has been
omitted.
 
K

Keith Thompson

Santosh , the message was supposed to be

struct service {
int sequence;
int port;
char names[NAMESIZE];
} hostsev;


struct servent {
char *s_name; /* official service name
*/
char **s_aliases; /* alias list */
int s_port; /* port number */
char *s_proto; /* protocol to use */
}


struct servent *h;
[snip]

Ok, stop right there.

The above code will not compile. There's a missing semicolon after
the declaration of "struct servent".

We have no way of knowing what other differences there are between the
code you posted and the code that's actually causing problems. The
actual problem could well be in something that you didn't post. We're
not going to waste our time trying to guess what the code *really*
looks like.

Post a small, complete, compilable program that illustrates the
problem. Don't try to re-type it; copy-and-paste it directly from a
source file that you've actually compiled. Don't post a single line
of code to be added to what you've already posted; post a complete
program.

And please read <http://cfaj.freeshell.org/google/> and
<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>.
 
B

Barry Schwarz

Santosh ,

struct service {
int sequence;
int port;
char names[NAMESIZE];
} hostsev;

struct service {
int sequence;
int port;
char names[NAMESIZE];
} hostsev;

You declare the struct service twice. You define the object hostsev
twice.
struct servent *h;

There is no struct servent.
while (h -> s_aliases != NULL)
{
printf("Alias %d : \t%s\n",i, h->s_aliases);
hostsev.names = * h->s_aliases;
printf("the content of the structure %s \n",
hostsev.names);
i++;
} // loop thru aliases

The first printf works , and the second doesn't which I believe means
that it's not storing
Thanks


In spite of your claim else thread to have forgotten nothing, you have
forgotten to show us your actual code.


Remove del for email
 
A

ataanis

Alright , Please accept my sincere appologies, here is the complete
code of my server side, I didn't want to post the whole code thinking
that it would be too much for people to read
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <string.h>
#include <netdb.h>


#define MYPORT 53491 /* the port users will be connecting to */

#define MSGSIZE 128

#define NAMESIZE 2

static int read_from_client (int fd, struct sockaddr_in *address);

/**
Accepts a string on the command line, sends that string to any
client that connects.
*/
int main (int argc, char *argv[])
{
char *message = "Waiting for another request";
int sockfd; /* datagram socket (2-way) */
struct sockaddr_in my_addr; /* my address */
int addr_size;
int enable = 1;
struct servent *h;
int port;
struct service {
int sequence;
int port;
char names[NAMESIZE];
} hostsev;



/* --- Check args and set up the message --- */
if (argc > 2) { /* usage message */
fprintf(stderr, "Usage: myserver port\n");
exit(1);
}
if (argc == 2) { /* take message from command line */
message = argv[1];
}

/* --- Create the socket --- */
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("Can't create socket");
exit(1);
}

/* --- Bind the socket, making its address reusable --- */
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPORT);
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
/* Note the use of INADDR_ANY to get the OS to use whatever local
interfaces are found. */

if (setsockopt(sockfd,
SOL_SOCKET, SO_REUSEADDR,
&enable, sizeof(int)) == -1) {
perror("Can't set socket option");
exit(1);
}

if (bind(sockfd,
(struct sockaddr *)&my_addr,
sizeof(my_addr)) == -1) {
perror("Could not bind");
exit(1);
}

/* --- No listen or accept code here - just wait for clients to make
requests --- */

/* --- Accept datagrams and serve our message --- */
while (1) {
struct sockaddr client_addr; /* client's address */
socklen_t addr_size = sizeof(struct sockaddr);
char client_buffer[MSGSIZE];
int readsz = 0;
char *name;
char *message1;
char *message2;
char *port_s;
char *protocol;
char *protocol1;
char **aliases;
//char *alias1;
int x = 1;
int i = 0;
int cnt;
char *SeqNumber;

/* Read from the client, get the client address filled in. Then we
can respond back to the same client. */
// memset(&client_addr, '\0', sizeof(struct sockaddr));
if ((readsz = recvfrom(sockfd, client_buffer, MSGSIZE, 0,&client_addr,
&addr_size)) > 0)
{
/* cast the client address to internet format for convenience */
struct sockaddr_in *in_addr = (struct sockaddr_in *)&client_addr;
client_buffer[readsz] = '\0';
/* take apart the address and see what we have, then print the
message */
fprintf(stderr, "Client: family=%d, port=%d, addr=%s,
length=%d\n",client_addr.sa_family,ntohs(in_addr->sin_port),inet_ntoa(in_addr->sin_addr),addr_size);

SeqNumber = strtok (client_buffer, ",");

name = strtok (NULL,",");
hostsev.sequence = atoi(SeqNumber);
printf(" there %i \n", hostsev.sequence);
while (1)
{
/* extract protocol from string sequence */
protocol = strtok(NULL,"\n");
fprintf(stderr,"Your request is the following: SeqNumber: %s, Name Of
Service: %s, Protocol: %s \n",SeqNumber,name,protocol);
//h = getservbyname(name,protocol);
if ((h = getservbyname(name,protocol)) == NULL)
{
printf("%s: unknown host %s \n",name,protocol);
exit(0);
}
else
{
printf("The port Number for your request is %d \n",ntohs(h
->s_port));
hostsev.port = h -> s_port;
printf("here %i \n" , ntohs(hostsev.port));

//port_s = atoi(itoa(ntohs(h -> s_port));
//exit(0);
i = 0;
hostsev.names[NAMESIZE] = 0 ;
while (h -> s_aliases != NULL)
{
//printf("Alias %d : \t%s\n",i, h->s_aliases);

for(cnt = 0; h->s_aliases != '\0'; ++cnt)
hostsev.names[cnt] = *h->s_aliases;
//hostsev.names += *h->s_aliases;
//printf("the content of the structure %s \n", hostsev.names);
i++;
} // loop thru aliases
break;
}
}

printf("the content of the structure %s \n", hostsev.names[0]);
/* Next, we write our message and close the socket. */
if (sendto(sockfd, &hostsev, sizeof(hostsev), 0,&client_addr,
addr_size) == -1)
{
perror("Failed to write to client");
}
else
{
fprintf(stderr, "%s\n", message);
}
}

else {
printf("Failed to read from client\n");
}
}

return 0;
}
Thank you
 
C

CBFalconer

Alright , Please accept my sincere appologies, here is the complete
code of my server side, I didn't want to post the whole code thinking
that it would be too much for people to read

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <string.h>
#include <netdb.h>
.... snip remaining non-standard code ...

Of the above, only stdio, stdlib, errno, and string are standard
include files. The rest are unknown quantities. This newsgroup
deals only with standard C, so you should adjust your code
accordingly before posting here, or alternatively go to a newsgroup
that deals with your system.

In addition, without context the whole thing is pointless. I would
assume that something doesn't work. There is no indication what.

In general on usenet you should realize that readers may very well
not have convenient access to previous articles in a thread. That
means that your reply articles should include adequate context, so
that they stand by themselves. Google is NOT usenet, it is only a
very poor interface to the real usenet system. To include proper
context when using google, see my sig. below. Please be sure to
read the referenced URLs.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,973
Members
47,529
Latest member
JaclynShum

Latest Threads

Top