strncpy difficulties

Hello all,

I have a problem using strncpy defined as:
char * strncpy ( char * destination, const char * source, size_t num );

The cursor get's lost or in other words it never proceeds in the terminal. It stops right during or right before strncpy execution.

Here the relevant part of the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
cout<<"\nenter new word\n";
			string my_string;
			cin>>my_string;
			/**char string[KEYWORD_MAX_LEN-1] ;
			cin.getline( string, KEYWORD_MAX_LEN-1);
			*/
			

			cout<<"\nnword is: "<<my_string<<"\n";

			int TempNumOne=my_string.size();
			char name[my_string.size()];
			for (int a=0;a<=TempNumOne;a++)
        	{
            	name[a]=my_string[a];
        	}
        	char *val = name;
			char y=*val;
			cout<< val<< "\nval is " <<val<<"\n";		//doesn't print

			//std::string s
		/**	char *a=new char[my_string.size()+1];
			a[my_string.size()]=0;
			memcpy(a,my_string.c_str(),my_string.size());
			char x=*a;
			cout<<"\ncharacter x is "<<x<<"\n";*/
		//	char str=my_string;

			char word[KEYWORD_MAX_LEN] = "";
			
			strncpy(word,&y,KEYWORD_MAX_LEN-1);


Now, enter -1 to quit
4

enter new word
hello

nword is: hello
hello
val is hello



Thank you.
Last edited on
char name[my_string.size()];
C++ wants you to use a constant for an array size. If you want to do dynamic memory allocation then lookup new and delete

for (int a=0;a<=TempNumOne;a++)
This for loop will iterate 1 past the number of elements needed

When you pass val to cout you're passing the memory address. * is used to dereference the pointer so you can pass what it's pointing to.

strncpy(word,&y,KEYWORD_MAX_LEN-1);
This is not the intended purpose of strncpy. You're passing it y as the source string which is just a single char and telling it to copy KEYWORK_MAX_LEN-1 elements from a single char to word. If you just want to set the first element of word to y then a simple assigment will do.
word[0] = y;
Ignore everything that was commented out from my code in the first post. By commented out, I mean code after " //"

I want to pass an equivalent of char* argv[ ] to my strncpy. Earlier in the code, it was passed through optarg, but I am not in main anymore and the value of "optarg" will be determined by me.
Prior code:
1
2
3
4
5
case 's':	//optarg is the keyword passed after s- optarg. i.e. the value is stored in optarg	
				cout<<"\noptarg is "<<optarg<<"\n";
				strncpy(word,optarg,KEYWORD_MAX_LEN-1);	//copies the first (KEYWORD_MAX_LEN-1) characters of optarg to word
				doSearch = true;
				break;


Basically, I want to implement the same, but with an arbitrary optarg outside of code main.
BTW, word is:
1
2
	char word[KEYWORD_MAX_LEN] = "";
 


Thank you.
Last edited on
I tried the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
while(i!=-1) {
			cout<<"search method about to exec";

			rcode = search(word);

			cout<<"\nNow, enter -1 to quit\n";// or word to continue\n";
			cin>>i;
			cout<<"\nenter new word\n";
		
			char my_stringC[100];
			cin>>my_stringC;

			cout<<"\nmystring is: "<<my_stringC<<"\n";

			cout<<"str copy about to exec with myStringC =  "<< my_stringC; //this doesnt execute
			strncpy(word,my_stringC,KEYWORD_MAX_LEN-1); //
		}

The cursor gets lost in the line before the last. It never executes that code.

enter -1 to end loop. otehrwise word to search
server says 'thnking' was *NOT* found.
server's answer was verified.
search method about to execinside search method
Now, enter -1 to quit
2

enter new word
hello

mystring is: hello



Any ideas?
There's no endl or \n on line 15. What you've output to cout is just sitting in a buffer and is not flushed to the terminal until you output an endl or \n.
Thank you all.
I found that the problem is greater than the endl- \n issue.

I need to start studying socket programming. Can anyone recommend a good book or free tutorial online for C++ socket programming?

Thank you again.
Topic archived. No new replies allowed.