0

Hi everyone,

I am trying to save ip addresses and mac addresses of a node in a structure.I dont know how to assign values.Please help.

#include <stdio.h>
#include<iostream>
#define nodes 15
using namespace std;

struct nodes_struct {
  char ip[19]; 
  char mac[19];
} ;

int main()
{
	char ip[19],mac[19];
	nodes_struct object[nodes];
	
		
		object[0].ip="192.168.1.2";//error 
		
	return 0;

}
3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by shashwat_2010
0

If you must use char arrays instead of strings you need to use strcpy(char* dest, char* source) from stdlib to assign values instead of the = operator you have now. With strings you could do str="abc".

0

Hi everyone,

I am trying to save ip addresses and mac addresses of a node in a structure.I dont know how to assign values.Please help.

#include <stdio.h>
#include<iostream>
#define nodes 15
using namespace std;

struct nodes_struct {
  char ip[19]; 
  char mac[19];
} ;

int main()
{
	char ip[19],mac[19];
	nodes_struct object[nodes];
	
		
		object[0].ip="192.168.1.2";//error 
		
	return 0;

}
#include <stdio.h>
#include <conio.h>
#include <memory.h>
#include <string.h>



struct nodes_struct 
{
  char ip[19]; 
  char mac[19];
} ;

void main()
{
	char *ptr="192.168.0.1";
	nodes_struct nodes[10];
	printf("%s\n",ptr);

	memset(nodes,0,sizeof(nodes_struct));
	memcpy(nodes[0].ip,ptr,strlen(ptr));
	printf("%s %s",ptr,nodes[0].ip);
	
	
	getchar();
}

Edited by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.