LinuxQuestions.org
Review your favorite Linux distribution.
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 08-08-2010, 06:08 AM   #1
rahulvishwakarma
Member
 
Registered: Aug 2010
Posts: 138

Rep: Reputation: 2
In c++ what should i use


#include<iostream>
#include<cstdio>
using namespace std;

main()
{
char *pch = new char[21];

snprintf(pch,11,"rahul");

/* what expession I should use in place
of snprintf in c++.
*/
cout<<pch<<endl;

delete pch;
return 0;
}
 
Old 08-08-2010, 07:13 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by rahulvishwakarma View Post
#include<iostream>
#include<cstdio>
using namespace std;

main()
{
char *pch = new char[21];

snprintf(pch,11,"rahul");

/* what expession I should use in place
of snprintf in c++.
*/
cout<<pch<<endl;

delete pch;
return 0;
}
'snprintf' expects a format string, doesn't it ? So, what is wrong with 'snprintf' as a function and why do you want a replacement for it in C++ ?

How about

man 3 snprintf

first ?
 
Old 08-08-2010, 12:22 PM   #3
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,223

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Code:
#include<iostream>
using namespace std;

main()
{
  string str = "rahul";
  cout << str << endl;
  return 0;
}
 
Old 08-08-2010, 12:24 PM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

1. This was your original (C) code, which crashed with a segmentation fault:
http://www.linuxquestions.org/questi...2/#post4056869
Code:
/*
  hey dude error in gnu c pointer please solve this
  I am using Red Hat Enterprise Linux 5.0.
 */
#include <stdio.h>
#include <stdlib.h>

int main()
{
  char * cp = NULL;
  cp = (char*)malloc(11);
  cp = "abcdefgh";
  printf(" cp = %s \n",cp);
  free(cp);
  return 0;
2. This was my response (to your C) question:
http://www.linuxquestions.org/questi...2/#post4056910
Code:
/*
 * Hey, dude - Movie recommendation
 */
...
  // 1) Declare character pointer - good!
  char * cp = NULL;

  // 2) Allocate space to hold up to 10 characters - good!
  cp = (char*)malloc(11);

  // 3) Assign a static string to the pointer.
  //    OK .. but you just undid step 2).  Why?
  //    And you just ORPHANED your allocated block - bad.  Memory leak!
  cp = "abcdefgh";

  // 4) Print the string - OK
  printf(" cp = %s \n",cp);

  // 5) Free the string - bad!
  //    cp now points to the static string - NOT the allocated block!
  free(cp);
...
3. And this is my response to your new, C++ only question:
Code:
#include<iostream>
#include<string>
using namespace std;

main()
{
  string s = "rahul";
  cout << s <<  endl;
  return 0;
}

Last edited by paulsm4; 08-08-2010 at 12:27 PM.
 
  


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



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

All times are GMT -5. The time now is 01:09 AM.

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