B
B
Boris the Animal2016-10-26 14:23:21
Programming
Boris the Animal, 2016-10-26 14:23:21

How, by passing a pointer to a function on a char inside the function, write a string there and return the number of characters in the string as well?

LATER ADDED:
I tried different options and just couldn't figure it out any further. If there is a tin somewhere, then just show how to do it. No need to explain to me that there is tin, I myself know. There is no time, like I don’t have it, and you have to suddenly waste it, so pass by, I don’t force you. The task is ridiculous, I’m just at work and I can’t be distracted by it, I haven’t been doing pluses for a very long time. Once again, I ask you, busy people, do not waste time, do not write nonsense to me in the comments, I do not force you to write something for me. The whole hardest task for half a minute!!!
---------------
Question:
Here he piled up, unfortunately there is no time to understand. I started to learn the pros a long time ago, but even everything has faded. I'll start over later, but now I need help :)

#include <stdlib.h>
#include <string>
#include <iostream>

using namespace std;

void securityLib_GetCode(const char** value, size_t* length)
{
  char* code = "CODE";
  *value = "CODE";

  char *arrPtr = &code[0];
  *length = strlen(arrPtr);
}

int main()
{
  const char* text = 0;
  size_t * length = 0;
  securityLib_GetCode(&text, length);

  cout << "Code = " << *text << endl;
  cout << "Code length = " << *length << endl;

  system("pause");
  return EXIT_SUCCESS;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly, 2016-10-26
@vt4a2h

std::string securityLib_GetCode()
{
   return "CODE";
}
// ...
auto code = securityLib_GetCode();
auto size = code.size();
auto raw = code.c_str();

Always use std::string as a wrapper around char *. Well, except for the obvious cases, for example, when this is the API of some module and trouble can happen due to different runtimes.

D
Daniil Demidko, 2016-10-27
@Daniro_San

No need to mix C++ with C!
First, stop using char* for strings as soon as possible.
Use std::string for this. Always use std::string for this.
And as a .NET programmer, it will be easier for you to use std::string, by analogy with System.String
Secondly, try to use C ++ libraries, not C
Thirdly, if you are already taking on pointers, then encapsulate them inside classes or do not use them at all.
Here's How You Can Rewrite Your Code in C++

#include <iostream>

void securityLib_GetCode(std::string &str, size_t &length) {
    const std::string key = "CODE";
    str= "CODE";
    length = key.size();
}

int main() {
  std::string text;
  size_t length = 0;
  securityLib_GetCode(text, length);
  cout << "Code = " << text << endl;
  cout << "Code length = " << length << endl;
}

It's another matter that the logic of the code itself seems a little meaningless)
Separately, I'll say about
It's just a lot of extra characters. Why take the address of the first element?
char *arrPtr = code; // Будет взят адрес текущего положения code

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question