Using Pointers, Arrays, and Structs in Programming

pointer arrays structs n.w
1 / 9
Embed
Share

Learn about the fundamentals of pointers, arrays, and structs in programming with examples and explanations. Understand how to pass data into printf/scanf functions, work with pointer arrays, declare and define structs, compare structs, and use pointers to structs efficiently in your code.

  • Data Types
  • Structs
  • Pointers
  • Arrays
  • Programming

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Pointer Arrays & Structs ENEE150 0102 ANDREW GOFFIN

  2. Project 2 Questions? Make sure you know what you re passing into printf/scanf: scanf can only take addresses/pointers printf takes values , except with pointer and strings, where it takes pointers Goffin ENEE150

  3. Pointer Arrays An array of pointers Can be denoted with double pointer char **argv == char *argv[] Can be dereferenced twice! Once to get a pointer value, and again to get the value stored at said pointer Most obvious application is command line arguments Goffin ENEE150

  4. Structures Compound datatypes Datatypes made up of primitive datatypes (int, char, float, etc.) Similar to arrays: Primitive data grouped together Can group data of different types! Useful for convenience and clarity of code Goffin ENEE150

  5. Declaring Structs struct example{ int ex1; char ex2; float ex3[10]; }; struct example var; var.ex1 = 0; var.ex2 = c ; // determine fields in struct in this declaration // fields can be of ANY datatype (including datatype of struct!) // access fields with . operator // the datatype is called struct example Goffin ENEE150

  6. Different ways to define structs /*define variable var from get-go */ struct example { } var; // easier declaration typedef struct example { } newtype, *ptype; newtype var; ptype pvar; // pointer to newtype Goffin ENEE150

  7. Various Struct Notes Comparing structs? Cannot do struct1 == struct 2 Must compare field-by-field Setting structs? CAN do struct1 = struct2 Does a field-by-field copy Goffin ENEE150

  8. Pointers to Structs Declare much like pointers to other datatypes Pointer points to first element in struct Accessing elements: (*pstruct).field accesses field Can be streamlined with the -> operator (*pstruct).field == pstruct->field Goffin ENEE150

  9. Structs and Functions Again, treat like other datatypes Structs can be used as parameters or return types struct example foo(struct example var); Takes struct as parameter and returns the same struct from the function Goffin ENEE150

More Related Content