0Pricing
Zig Academy · درس

مؤشرات C والسلاسل والأنواع

صِل بين نظامَي الأنواع في C وZig

مؤشرات C والسلاسل والأنواع درس مجاني في Zig Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Zig Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Zig Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

C Types Get Translated

When Zig imports C, each C type maps to a Zig one. An int becomes c_int, so widths match the C ABI exactly.

const x: c_int = 10;

The C ABI Integers

Zig provides ABI-matching names like c_int, c_uint, c_long, and c_char so your values line up with what C expects.

C Pointers Use [*c]

A raw C pointer becomes the special [*c]T type. It can be null and converts loosely, mirroring C's flexible pointers.

var p: [*c]u8 = null;

Why [*c] Is Special

The [*c] type bridges C's any-length, maybe-null pointers. It coerces to and from Zig slices and pointers where it can.

C Strings Are Bytes

A C string is just a pointer to bytes ending in a zero. There is no length stored, only the null terminator.

Null-Terminated Literals

Write a C-style string with the sentinel form. The type [*:0]const u8 marks bytes that end in a 0.

const s: [*:0]const u8 = "hello";

Pass Strings to C

Zig string literals are already null-terminated, so you can hand them straight to C functions expecting a char pointer.

c.puts("done");

Read a C String Back

To turn a C string into a Zig slice, use std.mem.span, which walks to the null byte to find the length.

const slice = std.mem.span(cstr);

void Pointers

C's void* shows up as ?*anyopaque in Zig. You cast it back to a concrete pointer type before using it.

Cast with @ptrCast

Bridging pointer types across the C boundary uses @ptrCast, which reinterprets a pointer without changing the address.

const p: *Node = @ptrCast(@alignCast(raw));

Match, Do Not Guess

The safe habit is to match C's exact types: use c_int for int and span for strings, so nothing is silently wrong.

Quick Check

Recall how Zig turns a C string into a usable slice.

Recap

You learned that C types map to ABI names like c_int, C pointers use [*c], and std.mem.span turns C strings into slices. 🎯

الأسئلة الشائعة

هل درس «مؤشرات C والسلاسل والأنواع» مجاني؟

نعم — نص درس «مؤشرات C والسلاسل والأنواع» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Zig Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Zig Academy 4 دروس في المجموع.

ماذا ستتعلم في «مؤشرات C والسلاسل والأنواع»؟

صِل بين نظامَي الأنواع في C وZig تتمرن على Zig Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Zig Academy؟

لا تُشترط خبرة سابقة. Zig Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.

كم من الوقت يستغرق درس «مؤشرات C والسلاسل والأنواع»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Zig Academy هذا؟

نعم. كل درس في Zig Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. استيراد ترويسات C باستخدام ‎@cImport‎
  2. استدعاء دوال C والربط
  3. مؤشرات C والسلاسل والأنواع
  4. ‏zig translate-c لقراءة C بصيغة Zig
← العودة إلى Zig Academy