5 Replies - 616 Views - Last Post: 27 May 2010 - 12:32 PM Rate Topic: -----

#1 Arkwind  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 28-November 08

Function Returning String?

Posted 27 May 2010 - 11:04 AM

Hi there - i'm trying to programme a function that when called returns a random 4 octet IP - but i keep getting errors and i have tried nearly everything - here is my code, please can someone tell me where to correct it.

#include <iostream>
#include <stdio.h>
#include <string>
#include <stdlib.h>
#include <time.h>
using namespace std;

char* ipgen();

char* ipgen(){
char buffer[50];
int oct1; // octet 1 of the random ip
int oct2; // octet 2 of the random ip
int oct3; // octet 3 of the random ip
int oct4; // octet 4 of the random ip
oct1 =rand() % 254 + 1;// random octet 1 
oct2 =rand() % 254 + 1;// random octet 2 
oct3 =rand() % 254 + 1;// random octet 3 
oct4 =rand() % 254 + 1;// random octet 4 
sprintf(buffer,"%d.%d.%d.%d \n",oct1,oct2,oct3,oct4);
return(buffer);
}

int main ()
{
cout << ipgen();
return(0);



thanks in advance - arkwind

Is This A Good Question/Topic? 0
  • +

Replies To: Function Returning String?

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Function Returning String?

Posted 27 May 2010 - 11:11 AM

Your function return-type a pointer right? Inside the function, you are returning the address of buffer which is a statically allocated array, therefore it will go out of scope after the function call. So, that address that you try to return, will not be available anymore. You could declare buffer as a pointer to char, then you would be able to return it.

This post has been edited by sarmanu: 27 May 2010 - 11:12 AM

Was This Post Helpful? 0
  • +
  • -

#3 Arkwind  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 28-November 08

Re: Function Returning String?

Posted 27 May 2010 - 11:12 AM

View Postsarmanu, on 27 May 2010 - 10:11 AM, said:

Your function return-type a pointer right? Inside the function, you are returning the address of buffer which is a statically allocated array, therefore it will go out of scope after the function call. So, that address that you try to return, will not be available anymore. You could make buffer a pointer to char, then return it.


Cheers - what would be the code to do this - sorry im mainly a vb programmmer

This post has been edited by Arkwind: 27 May 2010 - 11:12 AM

Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 885
  • View blog
  • Posts: 4,037
  • Joined: 09-June 09

Re: Function Returning String?

Posted 27 May 2010 - 11:17 AM

Quote

buffer which is a statically allocated array, therefore it will go out of scope after the function call


I dont think your meaning the work static since a statically allocated array will never go out of
scope, but we can call it automatic storage due to its short life span

Lets first talk about scope
A variables memory only exist within the scope where it was defined(unless static, or dynamic)
char* ipgen()
{ 
	char buffer[50]; //this memory was created in this scope, therefor it only exist in this scope
	return(buffer); //memory goes out of scope, your returing a bad pointer
} 



now for your problem. Your using the std:: namespace so lets use a std::string :)

Use a stringstream to convert the ints to strings and then just simply append the string
(Ill leave it to you to put the '.'s in)


not tested
std::string ipgen()
{ 
	std::string buffer; //will hold final ip
	
	for(int i=0; i<4; i++)
	{
		stringstream ss;
		ss<<rand() % 254 + 1;
		buffer.append(ss.str()); //append each number to the string
	}
	//ill let you put the periods in 
	return buffer; //creates a copy of the variable to return which scope exist in its return location
} 



read up here on different types of storage
http://www.exforsys....asses-in-c.html

This post has been edited by ImaSexy: 27 May 2010 - 12:41 PM

Was This Post Helpful? 1
  • +
  • -

#5 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Function Returning String?

Posted 27 May 2010 - 11:18 AM

Have a read here. Also, use a search engine and search for C++ pointers. I'm sure that you will find a lot of information.
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 4950
  • View blog
  • Posts: 11,358
  • Joined: 16-October 07

Re: Function Returning String?

Posted 27 May 2010 - 12:32 PM

The preferred method would be to pass the buffer:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define RO (rand() % 254 + 1)

void ipgen(char *buffer){
	sprintf(buffer,"%d.%d.%d.%d", RO, RO, RO, RO);
}

int main () {
	char buffer[16];
	ipgen(buffer);
	printf("%s\n", buffer);
	return 0;
}



However, you can pass a reference to a statically allocated block of memory:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define RO (rand() % 254 + 1)

const char* ipgen(){
	static char buffer[16]; // this location never goes away
	sprintf(buffer,"%d.%d.%d.%d", RO, RO, RO, RO);
	return buffer;
}

int main () {
	printf("%s\n", ipgen());
	return 0;
}




If you actually intended to use C++ (hard to tell from the headers and printf), then use a string:

#include <iostream>
#include <sstream>
#include <ctime>

using namespace std;

string ipgen(){
	stringstream ss;
	for(int i=0; i<4; i++) {
		if (i>0) { ss << '.'; }
		ss << (rand() % 254 + 1);
	}
	return ss.str();
}

int main () {
	cout << ipgen() << endl;
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1