3

I have this GSM module I got for my arduino project and I am trying to access the call feature, but the function makes no sense.

 boolean Adafruit_FONA::callPhone(char *number) {
 char sendbuff[35] = "ATD";
 strncpy(sendbuff+3, number, min(30, strlen(number)));
 uint8_t x = strlen(sendbuff);
 sendbuff[x] = ';';
 sendbuff[x+1] = 0;
 //Serial.println(sendbuff);
 return sendCheckReply(sendbuff, "OK");
 }

This is the function however how is char being used to display a phone number in the parameter?

Ignacio Vazquez-Abrams
17.7k1 gold badge28 silver badges32 bronze badges
asked Mar 12, 2015 at 21:06
1
  • This is bad code. It uses magic numbers (3, 30, and 35) instead of calculating values based on the actual sizes. The second call to strlen isn't needed; x is the sum of the length of the first string and the number of characters that were copied. Setting aside the incorrect use of strncpy that I mention in a comment below, if the input string is to long, the code truncates it and dials the truncated number! It shouldn't do that; it should return an error code. Don't try to learn programming from stuff like this. You'll pick up lots of bad habits. Commented Mar 13, 2015 at 5:59

2 Answers 2

3

It's passed as a char*. This can be derived from either a string literal (e.g. "212555121") or from a String via its c_str() method.

answered Mar 12, 2015 at 21:52
4
  • so if i wanted to use the function all i have to do is enter a string with the number inside? Commented Mar 13, 2015 at 20:11
  • If you want to use a fixed string then you can call it with the literal. If you have a char* with the string then you can pass it directly. If you have a String then you need to call the c_str() method. Arduino string reference Commented Mar 13, 2015 at 20:13
  • can u give an example of how i would create the char* with the string Commented Mar 14, 2015 at 1:57
  • What's wrong with how your posted code does it (other than that fiasco with strncpy(), of course)? Commented Mar 14, 2015 at 2:01
2

Keeping it simple:

char *number; a variable pointing to a character (or string of them).
char sendbuff[35]; 35 characters in a row, making a string.
The first 3 chars at locatiion sendbuff are initialised to "A", "T", & "D", then the string at "number" is copied to it (to a maximum of 30 characters, by taking the lesser of the length of "number" or 30. This is important to avoid over-running the space allocated.
This string of characters gets sent out; & a check is made for an "O" & a "K"

If you don't 'get' pointers, you need to look it up, as it's pretty central stuff.

answered Mar 12, 2015 at 22:54
1
  • 2
    I don't know why strncpy has become so popular as a "safe" replacement for strcpy. It isn't. If the number string is longer than 30 characters strncpy will copy the first 30 characters and stop without adding a NUL terminator. The subsequent call to strlen can then run off the end of the buffer, and if that happens, the two bytes subsequently stored will go outside the buffer. Commented Mar 13, 2015 at 5:07

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.