تعرف على كيفية إرجاع سلسلة من دالة C.
في أحد برامج C الخاصة بي ، كانت لدي مهمة إرجاع سلسلة من دالة:
xxxxx myName() {
return "Flavio";
}
الشيء الصعب هو تحديد نوع القيمة المعادة.
الأوتار في لغة C.نكونالمصفوفاتمنchar
العناصر ، لذلك لا يمكننا حقًا إرجاع سلسلة - يجب أن نعيد المؤشر إلى العنصر الأول من السلسلة.
هذا هو السبب في أننا بحاجة إلى استخدامconst char*
:
const char* myName() {
return "Flavio";
}
فيما يلي مثال لبرنامج العمل:
#include <stdio.h>
const char* myName() {
return "Flavio";
}
int main(void) {
printf("%s", myName());
}
إذا كنت تفضل إسناد النتيجة إلى متغير ، فيمكنك استدعاء الدالة كما يلي:
const char* name = myName();
يمكننا كتابةالمؤشرالمشغل أو العامل*
أيضًا بهذه الطرق:
const char * myName()
const char *myName()
جميع الأشكال صالحة تمامًا.
لاحظ استخدامconst
، لأنه من الوظيفة أعود أسلسلة حرفية، سلسلة معرفة بعلامات اقتباس مزدوجة ، وهي ثابتة.
لاحظ أنه لا يمكنك تعديل سلسلة حرفية في C.
شيء آخر يجب مراعاته هو أنه لا يمكنك إرجاع سلسلة معرفة كمتغير محلي من دالة C ، لأن المتغير سيتم إتلافه تلقائيًا (تحريره) عند انتهاء الوظيفة ، وبالتالي لن يكون متاحًا ، إذا حاولت على سبيل المثال القيام بما يلي:
#include <stdio.h>
const char* myName() {
char name[6] = "Flavio";
return name;
}
int main(void) {
printf("%s", myName());
}
سيكون لديك تحذير ، وبعض الهراء ، مثل هذا:
hello.c:5:10: warning: address of stack memory
associated with local variable 'name'
returned [-Wreturn-stack-address]
return name;
^~~~
1 warning generated.
B��Z> K���⏎Notice the B��Z> K���⏎
line at the end, which indicates that the memory that was first taken by the string now has been cleared and there’s other random data in that memory. So we don’t get the original string back.
Changing myName()
to
const char* myName() {
char *name = "Flavio";
return name;
}
makes the program work fine.
The reason is that variables are allocated on the stack, by default. But declaring a pointer, the value the pointers points to is allocated on the heap, and the heap is not cleared when the function ends.
Download my free C Handbook
More clang tutorials:
- Introduction to the C Programming Language
- C Variables and types
- C Constants
- C Operators
- C Conditionals
- How to work with loops in C
- Introduction to C Arrays
- How to determine the length of an array in C
- Introduction to C Strings
- How to find the length of a string in C
- Introduction to C Pointers
- Looping through an array with C
- Booleans in C
- Introduction to C Functions
- How to use NULL in C
- Basic I/O concepts in C
- Double quotes vs single quotes in C
- How to return a string from a C function
- How to solve the implicitly declaring library function warning in C
- How to check a character value in C
- How to print the percentage character using `printf()` in C
- C conversion specifiers and modifiers
- How to access the command line parameters in C
- Scope of variables in C
- Can you nest functions in C?
- Static variables in C
- C Global Variables
- The typedef keyword in C
- C Enumerated Types
- C Structures
- C Header Files
- The C Preprocessor