C programming_MSBTE_Diploma_Pranoti DokePranoti Doke
"1.1 Structure of ‘C’program, Assembler, Linker, Compiler, Interpreter.
1.2 ‘C’character set-keywords, identifiers, types of constants (Integer, single character, string, and real) variables, scope of variables, concept of ASCII.
1.3 Data types: integer- unsigned, signed, long, float- float, double, character char, string, octal, hexadecimal
1.4 Algorithm and flow chart.
1.5 Formatted input and output statements. Input and output function.
1.6 Operators and expressions:
a. Operators in ‘C’- arithmetic, logical, assignment, relational, increment and decrement, conditional, bit wise, special operators
b. Expressions
c. Precedence and associatively."
"2.1 Decision making if statement (if, if-else, nested if-else), switch –case statement.
2.2 Repetition in ‘C’ (loop control
statement) while, do-while and for loop, break and continue statement, nested loops
"
"3.1 Introduction to Array and its types
3.2 Declaration, initialization of array,
accessing elements of an array, adding,
deleting, sorting & searching.
3.3 Introduction to string Initializing,
declaring and display of string
3.4 String handling functions from standard library (strlen (), strcpy (), strcat (), strcmp(), strlwr(),strupr()):
"
"4.1 Concept and need of functions
4.2 Library functions: Math functions,
String handling functions, other
miscellaneous functions.
4.3 Writing User defined functions, scope of variables.
4.4 Parameter passing: call by value, call by reference.
4.5 Recursive functions
"
"5.1 Concept of pointer and pointer variables, initialization of pointer, call-by reference.
5.2 Pointer arithmetic.
5.3 Handling arrays using pointers
5.4 Handling functions using pointers
"
"6.1 Introduction and Features and Syntax of structure
6.2 Declaration and Initialization of
Structures
6.3 Initializing, assessing structure members using pointers
6.4 Type def, Enumerated Data Type,
using structures in C Program
6.5 Operations on structure."
C programming_MSBTE_Diploma_Pranoti DokePranoti Doke
"1.1 Structure of ‘C’program, Assembler, Linker, Compiler, Interpreter.
1.2 ‘C’character set-keywords, identifiers, types of constants (Integer, single character, string, and real) variables, scope of variables, concept of ASCII.
1.3 Data types: integer- unsigned, signed, long, float- float, double, character char, string, octal, hexadecimal
1.4 Algorithm and flow chart.
1.5 Formatted input and output statements. Input and output function.
1.6 Operators and expressions:
a. Operators in ‘C’- arithmetic, logical, assignment, relational, increment and decrement, conditional, bit wise, special operators
b. Expressions
c. Precedence and associatively."
"2.1 Decision making if statement (if, if-else, nested if-else), switch –case statement.
2.2 Repetition in ‘C’ (loop control
statement) while, do-while and for loop, break and continue statement, nested loops
"
"3.1 Introduction to Array and its types
3.2 Declaration, initialization of array,
accessing elements of an array, adding,
deleting, sorting & searching.
3.3 Introduction to string Initializing,
declaring and display of string
3.4 String handling functions from standard library (strlen (), strcpy (), strcat (), strcmp(), strlwr(),strupr()):
"
"4.1 Concept and need of functions
4.2 Library functions: Math functions,
String handling functions, other
miscellaneous functions.
4.3 Writing User defined functions, scope of variables.
4.4 Parameter passing: call by value, call by reference.
4.5 Recursive functions
"
"5.1 Concept of pointer and pointer variables, initialization of pointer, call-by reference.
5.2 Pointer arithmetic.
5.3 Handling arrays using pointers
5.4 Handling functions using pointers
"
"6.1 Introduction and Features and Syntax of structure
6.2 Declaration and Initialization of
Structures
6.3 Initializing, assessing structure members using pointers
6.4 Type def, Enumerated Data Type,
using structures in C Program
6.5 Operations on structure."
Functions: Function Definition, prototyping, types of functions, passing arguments to functions, Nested Functions, Recursive functions.
Strings: Declaring and Initializing strings, Operations on strings, Arrays of strings, passing strings to functions. Storage Classes: Automatic, External, Static and Register Variables.
Structures-Declaring and Initializing, Nested structure, Array of Structure, Passing Structures to functions, Unions, typedef, enum, Bit fields.
Pointers: Declarations, Pointer arithmetic, Pointers and functions, call by value, Call by reference, Pointers and Arrays, Arrays of Pointers, Pointers and Structures. Meaning of static and dynamic memory allocation, Memory allocation functions.
Files: File modes, File functions, and File operations, Text and Binary files, Command Line arguments Preprocessor directives. Macros: Definition, types of Macros, Creating and implementing user defined header files
1.History of C Language, Structure of a C program, Statements, Basic Data Types, Variables &Constants, Input & Output statements, Operators and Precedence, Expressions, Simple C programs.
The aim of this list of programming languages is to include all notable programming languages in existence, both those in current use and ... Note: This page does not list esoteric programming languages. .... Computer programming portal ...
Functions: Function Definition, prototyping, types of functions, passing arguments to functions, Nested Functions, Recursive functions.
Strings: Declaring and Initializing strings, Operations on strings, Arrays of strings, passing strings to functions. Storage Classes: Automatic, External, Static and Register Variables.
Structures-Declaring and Initializing, Nested structure, Array of Structure, Passing Structures to functions, Unions, typedef, enum, Bit fields.
Pointers: Declarations, Pointer arithmetic, Pointers and functions, call by value, Call by reference, Pointers and Arrays, Arrays of Pointers, Pointers and Structures. Meaning of static and dynamic memory allocation, Memory allocation functions.
Files: File modes, File functions, and File operations, Text and Binary files, Command Line arguments Preprocessor directives. Macros: Definition, types of Macros, Creating and implementing user defined header files
1.History of C Language, Structure of a C program, Statements, Basic Data Types, Variables &Constants, Input & Output statements, Operators and Precedence, Expressions, Simple C programs.
The aim of this list of programming languages is to include all notable programming languages in existence, both those in current use and ... Note: This page does not list esoteric programming languages. .... Computer programming portal ...
A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before you can use it to store any variable address.
There are few important operations, which we will do with the help of pointers very frequently. (a) we define a pointer variable (b) assign the address of a variable to a pointer and (c) finally access the value at the address available in the pointer variable. This is done by using unary operator * that returns the value of the variable located at the address specified by its operand.
This is an intermediate conversion course for C++, suitable for second year computing students who may have learned Java or another language in first year.
In computer science, a pointer is a programming language object, whose value refers to (or "points to") another value stored elsewhere in the computer memory using its memory address. A pointer references a location in memory, and obtaining the value stored at that location is known as dereferencing the pointer.
To make this comparison we need to first consider the problem that both approaches help us to solve. When programming any system you are essentially dealing with data and the code that changes that data. These two fundamental aspects of programming are handled quite differently in procedural systems compared with object oriented systems, and these differences require different strategies in how we think about writing code.
this presentation is made for the students who finds data structures a complex subject
this will help students to grab the various topics of data structures with simple presentation techniques
best regards
BCA group
(pooja,shaifali,richa,trishla,rani,pallavi,shivani)
Programming Fundamentals Arrays and Strings imtiazalijoono
Programming Fundamentals
Arrays and Strings
• Arrays
• Initializing arrays
• Multidimensional arrays
• Arrays as arguments to functions
• Strings
• String functions
Accessing Individual Components
Declaring Arrays
Arrays: Example Garbage
Multidimensional Arrays
1) Write a C program using arrays that produces the multiplication of two 2x2 matrices.
3-Dimensional Array
Write a program of your own choice that makes use of arrays of more than 2 dimensions.
2-Dimensional Arrays
At the end of this lecture students should be able to;
Describe the C arrays.
Practice the declaration, initialization and access linear arrays.
Practice the declaration, initialization and access two dimensional arrays.
Apply taught concepts for writing programs.
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisGlobus
JASMIN is the UK’s high-performance data analysis platform for environmental science, operated by STFC on behalf of the UK Natural Environment Research Council (NERC). In addition to its role in hosting the CEDA Archive (NERC’s long-term repository for climate, atmospheric science & Earth observation data in the UK), JASMIN provides a collaborative platform to a community of around 2,000 scientists in the UK and beyond, providing nearly 400 environmental science projects with working space, compute resources and tools to facilitate their work. High-performance data transfer into and out of JASMIN has always been a key feature, with many scientists bringing model outputs from supercomputers elsewhere in the UK, to analyse against observational or other model data in the CEDA Archive. A growing number of JASMIN users are now realising the benefits of using the Globus service to provide reliable and efficient data movement and other tasks in this and other contexts. Further use cases involve long-distance (intercontinental) transfers to and from JASMIN, and collecting results from a mobile atmospheric radar system, pushing data to JASMIN via a lightweight Globus deployment. We provide details of how Globus fits into our current infrastructure, our experience of the recent migration to GCSv5.4, and of our interest in developing use of the wider ecosystem of Globus services for the benefit of our user community.
Understanding Globus Data Transfers with NetSageGlobus
NetSage is an open privacy-aware network measurement, analysis, and visualization service designed to help end-users visualize and reason about large data transfers. NetSage traditionally has used a combination of passive measurements, including SNMP and flow data, as well as active measurements, mainly perfSONAR, to provide longitudinal network performance data visualization. It has been deployed by dozens of networks world wide, and is supported domestically by the Engagement and Performance Operations Center (EPOC), NSF #2328479. We have recently expanded the NetSage data sources to include logs for Globus data transfers, following the same privacy-preserving approach as for Flow data. Using the logs for the Texas Advanced Computing Center (TACC) as an example, this talk will walk through several different example use cases that NetSage can answer, including: Who is using Globus to share data with my institution, and what kind of performance are they able to achieve? How many transfers has Globus supported for us? Which sites are we sharing the most data with, and how is that changing over time? How is my site using Globus to move data internally, and what kind of performance do we see for those transfers? What percentage of data transfers at my institution used Globus, and how did the overall data transfer performance compare to the Globus users?
Check out the webinar slides to learn more about how XfilesPro transforms Salesforce document management by leveraging its world-class applications. For more details, please connect with sales@xfilespro.com
If you want to watch the on-demand webinar, please click here: https://www.xfilespro.com/webinars/salesforce-document-management-2-0-smarter-faster-better/
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteGoogle
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
👉👉 Click Here To Get More Info 👇👇
https://sumonreview.com/ai-pilot-review/
AI Pilot Review: Key Features
✅Deploy AI expert bots in Any Niche With Just A Click
✅With one keyword, generate complete funnels, websites, landing pages, and more.
✅More than 85 AI features are included in the AI pilot.
✅No setup or configuration; use your voice (like Siri) to do whatever you want.
✅You Can Use AI Pilot To Create your version of AI Pilot And Charge People For It…
✅ZERO Manual Work With AI Pilot. Never write, Design, Or Code Again.
✅ZERO Limits On Features Or Usages
✅Use Our AI-powered Traffic To Get Hundreds Of Customers
✅No Complicated Setup: Get Up And Running In 2 Minutes
✅99.99% Up-Time Guaranteed
✅30 Days Money-Back Guarantee
✅ZERO Upfront Cost
See My Other Reviews Article:
(1) TubeTrivia AI Review: https://sumonreview.com/tubetrivia-ai-review
(2) SocioWave Review: https://sumonreview.com/sociowave-review
(3) AI Partner & Profit Review: https://sumonreview.com/ai-partner-profit-review
(4) AI Ebook Suite Review: https://sumonreview.com/ai-ebook-suite-review
Enhancing Research Orchestration Capabilities at ORNL.pdfGlobus
Cross-facility research orchestration comes with ever-changing constraints regarding the availability and suitability of various compute and data resources. In short, a flexible data and processing fabric is needed to enable the dynamic redirection of data and compute tasks throughout the lifecycle of an experiment. In this talk, we illustrate how we easily leveraged Globus services to instrument the ACE research testbed at the Oak Ridge Leadership Computing Facility with flexible data and task orchestration capabilities.
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxrickgrimesss22
Discover the essential features to incorporate in your Winzo clone app to boost business growth, enhance user engagement, and drive revenue. Learn how to create a compelling gaming experience that stands out in the competitive market.
Accelerate Enterprise Software Engineering with PlatformlessWSO2
Key takeaways:
Challenges of building platforms and the benefits of platformless.
Key principles of platformless, including API-first, cloud-native middleware, platform engineering, and developer experience.
How Choreo enables the platformless experience.
How key concepts like application architecture, domain-driven design, zero trust, and cell-based architecture are inherently a part of Choreo.
Demo of an end-to-end app built and deployed on Choreo.
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Globus
Large Language Models (LLMs) are currently the center of attention in the tech world, particularly for their potential to advance research. In this presentation, we'll explore a straightforward and effective method for quickly initiating inference runs on supercomputers using the vLLM tool with Globus Compute, specifically on the Polaris system at ALCF. We'll begin by briefly discussing the popularity and applications of LLMs in various fields. Following this, we will introduce the vLLM tool, and explain how it integrates with Globus Compute to efficiently manage LLM operations on Polaris. Attendees will learn the practical aspects of setting up and remotely triggering LLMs from local machines, focusing on ease of use and efficiency. This talk is ideal for researchers and practitioners looking to leverage the power of LLMs in their work, offering a clear guide to harnessing supercomputing resources for quick and effective LLM inference.
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar
The European Union Agency for Law Enforcement Cooperation (Europol) has suffered an alleged data breach after a notorious threat actor claimed to have exfiltrated data from its systems. Infamous data leaker IntelBroker posted on the even more infamous BreachForums hacking forum, saying that Europol suffered a data breach this month.
The alleged breach affected Europol agencies CCSE, EC3, Europol Platform for Experts, Law Enforcement Forum, and SIRIUS. Infiltration of these entities can disrupt ongoing investigations and compromise sensitive intelligence shared among international law enforcement agencies.
However, this is neither the first nor the last activity of IntekBroker. We have compiled for you what happened in the last few days. To track such hacker activities on dark web sources like hacker forums, private Telegram channels, and other hidden platforms where cyber threats often originate, you can check SOCRadar’s Dark Web News.
Stay Informed on Threat Actors’ Activity on the Dark Web with SOCRadar!
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...Juraj Vysvader
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I didn't get rich from it but it did have 63K downloads (powered possible tens of thousands of websites).
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Globus
The Earth System Grid Federation (ESGF) is a global network of data servers that archives and distributes the planet’s largest collection of Earth system model output for thousands of climate and environmental scientists worldwide. Many of these petabyte-scale data archives are located in proximity to large high-performance computing (HPC) or cloud computing resources, but the primary workflow for data users consists of transferring data, and applying computations on a different system. As a part of the ESGF 2.0 US project (funded by the United States Department of Energy Office of Science), we developed pre-defined data workflows, which can be run on-demand, capable of applying many data reduction and data analysis to the large ESGF data archives, transferring only the resultant analysis (ex. visualizations, smaller data files). In this talk, we will showcase a few of these workflows, highlighting how Globus Flows can be used for petabyte-scale climate analysis.
Large Language Models and the End of ProgrammingMatt Welsh
Talk by Matt Welsh at Craft Conference 2024 on the impact that Large Language Models will have on the future of software development. In this talk, I discuss the ways in which LLMs will impact the software industry, from replacing human software developers with AI, to replacing conventional software with models that perform reasoning, computation, and problem-solving.
Cyaniclab : Software Development Agency Portfolio.pdfCyanic lab
CyanicLab, an offshore custom software development company based in Sweden,India, Finland, is your go-to partner for startup development and innovative web design solutions. Our expert team specializes in crafting cutting-edge software tailored to meet the unique needs of startups and established enterprises alike. From conceptualization to execution, we offer comprehensive services including web and mobile app development, UI/UX design, and ongoing software maintenance. Ready to elevate your business? Contact CyanicLab today and let us propel your vision to success with our top-notch IT solutions.
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamtakuyayamamoto1800
In this slide, we show the simulation example and the way to compile this solver.
In this solver, the Helmholtz equation can be solved by helmholtzFoam. Also, the Helmholtz equation with uniformly dispersed bubbles can be simulated by helmholtzBubbleFoam.
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns
Unlocking Business Potential: Tailored Technology Solutions by Prosigns
Discover how Prosigns, a leading technology solutions provider, partners with businesses to drive innovation and success. Our presentation showcases our comprehensive range of services, including custom software development, web and mobile app development, AI & ML solutions, blockchain integration, DevOps services, and Microsoft Dynamics 365 support.
Custom Software Development: Prosigns specializes in creating bespoke software solutions that cater to your unique business needs. Our team of experts works closely with you to understand your requirements and deliver tailor-made software that enhances efficiency and drives growth.
Web and Mobile App Development: From responsive websites to intuitive mobile applications, Prosigns develops cutting-edge solutions that engage users and deliver seamless experiences across devices.
AI & ML Solutions: Harnessing the power of Artificial Intelligence and Machine Learning, Prosigns provides smart solutions that automate processes, provide valuable insights, and drive informed decision-making.
Blockchain Integration: Prosigns offers comprehensive blockchain solutions, including development, integration, and consulting services, enabling businesses to leverage blockchain technology for enhanced security, transparency, and efficiency.
DevOps Services: Prosigns' DevOps services streamline development and operations processes, ensuring faster and more reliable software delivery through automation and continuous integration.
Microsoft Dynamics 365 Support: Prosigns provides comprehensive support and maintenance services for Microsoft Dynamics 365, ensuring your system is always up-to-date, secure, and running smoothly.
Learn how our collaborative approach and dedication to excellence help businesses achieve their goals and stay ahead in today's digital landscape. From concept to deployment, Prosigns is your trusted partner for transforming ideas into reality and unlocking the full potential of your business.
Join us on a journey of innovation and growth. Let's partner for success with Prosigns.
2. References
• www.stackoverflow.com
• C Primer Plus 6th Edition
• Let Us C 5th Edition
• The C Programming Language 2nd Edition
• C Modern Approach 2nd Edition
• Data Structures and Algorithm Analysis in C 2nd Edition
Mo’meN M. Ali
C Programming Language
3. Review Programs are uploaded to this Git repo
https://github.com/Mo2meN-
Ali/x86/tree/master/Programming%20Course/1-
Arrays%20%26%20Pointers
C Programming Language
Mo’meN M. Ali
4. Topics
• Arrays & Pointers.
• Character String & String Functions.
• Storage Classes, Linkage & Memory Management.
• File Input/Output.
• Structures & Other Data Formats.
• Bit Fiddling.
• The C Preprocessor & The C Library.
• Algorithm Analysis & ADT.
• Stacks & Queues.
• Trees.
C Programming Language
Mo’meN M. Ali
5. Today You Will Learn About:
• Keyword: static.
• Operators: & * (unary).
• How to create and initialize arrays.
• Pointers.
• Writing functions that process arrays.
• Two-dimensional arrays.
C Programming Language
Mo’meN M. Ali
6. Arrays
• An array is composed of a series of elements of one data type.
• An array declaration tells the compiler how many elements the array
contains and what the type is for these elements.
• To access elements in an array, you identify an individual element by using
its subscript number, also called its index,The numbering starts with 0.
C Programming Language
Mo’meN M. Ali
/* some array declarations */
int main(void)
{
float candy[365]; /* array of 365 floats */
char code[12]; /* array of 12 chars */
int states[50]; /* array of 50 ints */
}
7. C Programming Language
Mo’meN M. Ali
#include <stdio.h>
#define MONTHS 12
int main(void)
{
int days[MONTHS]= {31,28,31,30,31,30,31,31,30,31,30,31};
int index;
for (index = 0; index < MONTHS; index++)
printf("Month %d has %2d days.n", index +1, days[index]);
return 0;
}
8. Output
Month 1 has 31 days.
Month 2 has 28 days.
Month 3 has 31 days.
Month 4 has 30 days.
Month 5 has 31 days.
Month 6 has 30 days.
Month 7 has 31 days.
Month 8 has 31 days.
Month 9 has 30 days.
Month 10 has 31 days.
Month 11 has 30 days.
Month 12 has 31 days.
C Programming Language
Mo’meN M. Ali
9. Entering Data into an Array
for ( i = 0 ; i <= 29 ; i++ )
{
printf ( "nEnter marks " ) ;
scanf ( "%d", &states[i] ) ;
}
C Programming Language
Mo’meN M. Ali
Reading data from an array
for ( i = 0 ; i <= 29 ; i++ )
sum= sum + marks[i] ;
10. C Programming Language
Mo’meN M. Ali
#include <stdio.h>
#define SIZE 4
int main(void)
{
int some_data[SIZE]= {1492, 1066};
int i;
printf("%2s%14sn", i, some_data[i]);
for (i = 0; i < SIZE; i++)
printf("%2d%14dn", i, some_data[i]);
return 0;
}
12. C Programming Language
Mo’meN M. Ali
#include <stdio.h>
int main(void)
{
const int days[]= {31,28,31,30,31,30,31,31,30,31};
int index;
for (index= 0; index < ( sizeof (days) / sizeof (days[0]) ); index++)
printf("Month %2d has %d days.n", index +1, days[index]);
return 0;
}
13. Output
Month 1 has 31 days.
Month 2 has 28 days.
Month 3 has 31 days.
Month 4 has 30 days.
Month 5 has 31 days.
Month 6 has 30 days.
Month 7 has 31 days.
Month 8 has 31 days.
Month 9 has 30 days.
Month 10 has 31 days.
C Programming Language
Mo’meN M. Ali
14. What if you fail to initialize an array?
#include <stdio.h>
#define SIZE 4
int main(void)
{
int no_data[SIZE]; /* uninitialized array */
int i;
printf("%2s%14sn", "i", "no_data[i]");
for (i = 0; i < SIZE; i++)
printf("%2d%14dn", i, no_data[i]);
return 0;
}
C Programming Language
Mo’meN M. Ali
15. Output (Your Results may vary)
i no_data[i]
0 0
1 4204937
2 4219854
3 2147348480
C Programming Language
Mo’meN M. Ali
16. Array Bounds
• You have to make sure you use array indices that are within bounds; that
is, you have to make sure they have values valid for the array. For
instance, suppose you make the following declaration:
int doofi[20];
• Then it's your responsibility to make sure the program uses indices only in
the range 0 through 19, because the compiler won't check for you.
C Programming Language
Mo’meN M. Ali
17. C Programming Language
Mo’meN M. Ali
#include <stdio.h>
#define SIZE 4
int main(void)
{
int value1 = 44;
int arr[SIZE];
int value2 = 88;
int i;
printf("value1 = %d, value2 = %dn", value1, value2);
for (i = -1; i <= SIZE; i++)
arr[i] = 2 * i + 1;
for (i = -1; i < 7; i++)
printf("%2d %dn", i , arr[i]);
printf("value1 = %d, value2 = %dn", value1, value2);
return 0;
}
18. Output
value1 = 44, value2 = 88
-1 -1
0 1
1 3
2 5
3 7
4 9
5 5
6 1245120
value1 = -1, value2 = 9
C Programming Language
Mo’meN M. Ali
19. Designated Initializers (C99)
• C99 has added a new capability designated initializers. This feature allows
you to pick and choose which elements are initialized. Suppose, for
example, that you just want to initialize the last element in an array. With
traditional C initialization syntax, you also have to initialize every element
preceding the last one:
int arr[6] = {0,0,0,0,0,212}; // traditional syntax
• With C99, you can use an index in brackets in the initialization list to specify
a particular element:
int arr[6] = {[5] = 212}; // initialize arr[5] to 212
C Programming Language
Mo’meN M. Ali
20. C Programming Language
Mo’meN M. Ali
// designate.c -- use designated initializers
#include <stdio.h>
#define MONTHS 12
int main(void)
{
int days[MONTHS] = {31,28, [4] = 31,30,31, [1] = 29};
int i;
for (i = 0; i < MONTHS; i++)
printf("%2d %dn", i + 1, days[i]);
return 0;
}
21. Output
1 31
2 29
3 0
4 0
5 31
6 30
7 31
8 0
9 0
10 0
11 0
12 0
C Programming Language
Mo’meN M. Ali
22. Suppose you don’t specify the
array size?
int stuff[] = {1, [6] = 23}; // what happens?
int staff[] = {1, [6] = 4, 9, 10}; // what happens?
• The compiler will make the array big enough to accommodate the
initialization values. So stuff will have seven elements, numbered 0-6, and
staff will have two more elements, or 9.
C Programming Language
Mo’meN M. Ali
23. Specifying an Array Size
#define SIZE 4
int arr[SIZE]; // symbolic integer constant
double lots[144]; // literal integer constant
int n = 5;
int m = 8;
float a1[5]; // yes
float a2[5*2 + 1]; // yes
float a3[sizeof(int) + 1]; // yes
float a4[-4]; // no, size must be > 0
float a5[0]; // no, size must be > 0
float a6[2.5]; // no, size must be an integer
float a7[(int)2.5]; // yes, typecast float to int constant
float a8[n]; // not allowed before C99
float a9[m]; // not allowed before C99
C Programming Language
Mo’meN M. Ali
25. Multidimensional arrays
• A multidimensional array is an array which every element in it is an array.
float rain[5] [12]; // an array of 12 floats
• This tells us that each element is of type float[12]; that is, each of the five
elements of rain is, in itself, an array of 12 float values.
C Programming Language
Mo’meN M. Ali
26. Initializing a two-dimensional array
#define MONTHS 12 // number of months in a year
#define YEARS 5 // number of years of data
const float rain[YEARS][MONTHS] = {
{4.3,4.3,4.3,3.0,2.0,1.2,0.2,0.2,0.4,2.4,3.5,6.6}, // Row 0 (rain[0])
{8.5,8.2,1.2,1.6,2.4,0.0,5.2,0.9,0.3,0.9,1.4,7.3}, // Row 1 (rain[1])
{9.1,8.5,6.7,4.3,2.1,0.8,0.2,0.2,1.1,2.3,6.1,8.4}, // Row 2 (rain[2])
{7.2,9.9,8.4,3.3,1.2,0.8,0.4,0.0,0.6,1.7,4.3,6.2}, // Row 3 (rain[3])
{7.6,5.6,3.8,2.8,3.8,0.2,0.0,0.0,0.0,1.3,2.6,5.2} // Row 4 (rain[4])
};
Review Program: rain
C Programming Language
Mo’meN M. Ali
27. Pointers And Arrays
• Pointers offer an efficient way to deal with arrays. Indeed.
• Array notation is simply a disguised use of pointers.
1. date == &date[0]; // name of array is the address of the first element
2. dates +2 == &date[2] /* same address */
3. *(dates + 2) == dates[2] /* same value */
4. *(dates +2) /* value of the 3rd element of dates */
5. *dates +2 /* 2 added to the value of the 1st element */
C Programming Language
Mo’meN M. Ali
28. C Programming Language
Mo’meN M. Ali
#include <stdio.h>
#define SIZE 4
int main(void)
{
short dates [SIZE];
short * pti;
short index;
double bills[SIZE];
double * ptf;
pti = dates; // assign address of array to pointer
ptf = bills;
printf("%23s %15sn", "short", "double");
for (index = 0; index < SIZE; index ++)
printf("pointers + %d: %10p %10pn", index, pti + index, ptf + index);
return 0;
}
29. Output
short double
pointers + 0: 0x7fff5fbff8dc 0x7fff5fbff8a0
pointers + 1: 0x7fff5fbff8de 0x7fff5fbff8a8
pointers + 2: 0x7fff5fbff8e0 0x7fff5fbff8b0
pointers + 3: 0x7fff5fbff8e2 0x7fff5fbff8b8
C Programming Language
Mo’meN M. Ali
30. C Programming Language
Mo’meN M. Ali
/* day_mon3.c -- uses pointer notation */
#include <stdio.h>
#define MONTHS 12
int main(void)
{
int days[MONTHS] = {31,28,31,30,31,30,31,31,30,31,30,31};
int index;
for (index = 0; index < MONTHS; index++)
printf("Month %2d has %d days.n",
index +1, *(days + index)); // same as days[index]
return 0;
}
31. Functions, Arrays, and Pointers
• Suppose you want to write a function that operates on an array. For example,
suppose you want a function that returns the sum of the elements of an
array. Suppose marbles is the name of an array of int. What would the
function call look like? A reasonable guess would be this:
total = sum(marbles); // possible function call
• What would the prototype be? Remember, the name of an array is the
address of its first element,
int sum(int * ar); // corresponding prototype
C Programming Language
Mo’meN M. Ali
32. C Programming Language
Mo’meN M. Ali
int sum(int * ar) // corresponding definition
{
int i;
int total = 0;
for( i = 0; i < 10; i++) // assume 10 elements
total += ar[i]; // ar[i] the same as *(ar + i)
return total;
}
int sum(int * ar, int n) // more general approach
{
int i;
int total = 0;
for( i = 0; i < n; i++) // use n elements
total += ar[i]; // ar[i] the same as *(ar + i)
return total;
}
33. Using Pointer Parameters
• A function working on an array needs to know where to start and stop.
• Another way to describe the array is by passing two pointers, with the first
indicating where the array starts (as before) and the second where the array
ends.
• Now, the function can alter the value of the pointer itself, making it point to
each array element in turn.
C Programming Language
Mo’meN M. Ali
34. Arrays as Arguments
• Because the name of an array is the address of the first element, an actual
argument of an array name requires that the matching formal argument be a
pointer. Also, C interprets int ar[] to mean the same as int * ar.
• All four of the following prototypes are equivalent:
int sum(int *ar, int n);
int sum(int *, int);
int sum(int ar[], int n);
int sum(int [], int);
Review Program: Array as arguments
C Programming Language
Mo’meN M. Ali
35. Pointer Operations
Review Program: PointerOperations
• Assignment— You can assign an address to a pointer. The assigned value can be, for
example, an array name, a variable preceded by address operator ( & ), or another second
pointer.
• Value finding (dereferencing)— The * operator gives the value stored in the pointed-to
location. Therefore, *ptr1 is initially 100 , the value stored at location 0x7fff5fbff8d0 .
• Taking a pointer address— Like all variables, a pointer variable has an address and a value.
The & operator tells you where the pointer itself is stored.
• Adding an integer to a pointer— You can use the + operator to add an integer to a pointer
or a pointer to an integer. In either case, the integer is multiplied by the number of bytes in
the pointed-to type, and the result is added to the original address. This makes ptr1 + 4 the
same as &urn[4] .
C Programming Language
Mo’meN M. Ali
36. • Incrementing a pointer— Incrementing a pointer to an array element makes it move to the
next element of the array. Therefore, ptr1++ increases the numerical value of ptr1 by 4 (4
bytes per int on our system) and makes ptr1 point to urn[1].
• Subtracting an integer from a pointer— You can use the - operator to subtract an integer
from a pointer; the pointer has to be the first operand and the integer value the second
operand. The integer is multiplied by the number of bytes in the pointed-to type, and the
result is subtracted from the original address.
• Decrementing a pointer— Of course, you can also decrement a pointer. In this example,
decrementing ptr2 makes it point to the second array element instead of the third. Note
that you can use both the prefix and postfix forms of the increment and decrement
operators.
• Differencing— You can find the difference between two pointers. Normally, you do this for
two pointers to elements that are in the same array to find out how far apart the elements
are. The result is in the same units as the type size.
• Comparisons— You can use the relational operators to compare the values of two pointers,
provided the pointers are of the same type.
C Programming Language
Mo’meN M. Ali
37. Using const with formal
parameters
• If a function is intent is that it not change the contents of the array, use the keyword
const when declaring the formal parameter in the prototype and in the function
definition.
C Programming Language
Mo’meN M. Ali
38. Pointers to multidimensional
Arrays
• Since 2D-Arrays are Arrays of arrays, therefore we need a pointer-to-array instead of a
pointer-to-element.
int (* pz)[2]; // pz points to an array of 2 ints. (Can be used as 2D
// Array pointer).
int * pax[2]; // pax is an array of two pointers-to-int (Can no be used as 2D
// Array Pointer).
C Programming Language
Mo’meN M. Ali
39. Pointer Compatibility
• The rules for assigning one pointer to another are tighter than the rules for numeric
types. For example, you can assign an int value to a double variable without using a
type conversion, but you can’t do the same for pointers to these two types:
int n= 5;
double x;
int * p1= &n;
double * pd= &x;
x= n; // implicit type conversion
pd= p1; // compile-time error
C Programming Language
Mo’meN M. Ali
40. int * pt;
int (*pa)[3];
int ar1[2][3];
int ar2[3][2];
int **p2; // a pointer to a pointer
pt = &ar1[0][0]; // both pointer-to-int
pt = ar1[0]; // both pointer-to-int
pt = ar1; // not valid
pa = ar1; // both pointer-to-int[3]
pa = ar2; // not valid
p2 = &pt; // both pointer-to-int *
*p2 = ar2[0]; // both pointer-to-int
p2 = ar2; // not valid
Review Program: Pointers and 2D-Arrays
C Programming Language
Mo’meN M. Ali
41. Functions and multidimensional
arrays
• If you want to write functions that process two-dimensional arrays, you need to
understand pointers well enough to make the proper declarations for function
arguments. In the function body itself, you can usually get by with array notation.
int junk[3][4] = { {2,4,5,8}, {3,5,6,9}, {12,10,8,6} };
int i, j;
int total = 0;
for (i = 0; i < 3 ; i++)
total += sum(junk[i], 4); // junk[i] -- one-dimensional array
C Programming Language
Mo’meN M. Ali
43. Variable-length arrays (VLAs)
• You may have noticed that you can not use a variable size multidimensional array as an
argument, that is because you always have to use a constant columns. Well VLAs is the
C99 way to solve this problem. You can use VLAs to pass a variable length
multidimensional array.
int sum2d(int rows, int cols, int ar[rows][cols]); // array a VLA
int sum2d(int, int, int ar[*][*]); // array a VLA, names omitted
• rows and cols are two variable arguments that can be passed at run-time.
• rows and cols must be define before the array.
int sum2d(int ar[rows][cols], int rows, int cols); // invalid order
Review Program: Functions usingVLAs
C Programming Language
Mo’meN M. Ali
44. More dimensions
• Everything we have said about two-dimensional arrays can be generalized
to three-dimensional arrays and further. You can declare a three-
dimensional array this way:
int box[10][20][30];
• You can visualize a one-dimensional array as a row of data, a two-
dimensional array as a table of data, and a three-dimensional array as a
stack of data tables. For example, you can visualize the box array as 10
two-dimensional arrays (each 20×30) stacked atop each other.
C Programming Language
Mo’meN M. Ali