Solved

Simple two dimensional array question

Posted on 2002-04-14
7
186 Views
Last Modified: 2010-04-15
Does this place a copy of the two dim pointer on the stack or does it copy the entire two dim array on the stack (MSVC 6.0)?


#include <string.h>
#include <stdio.h>
#include <stdlib.h>

void go2(char [][80]);
int main(int argc, char* argv[])
{
     char lineptr[5][80];
     strcpy(lineptr[0], "ron");
     go2(lineptr);
     printf("%s\n", lineptr[0]);
     return 0;
}

void go2(char arr[][80])
//void go2(char *arr[])
{
     strcpy(arr[0], "h");
     printf("%s\n", arr[0]);
     return;
}
0
Comment
Question by:ronandersen
7 Comments
 
LVL 10

Accepted Solution

by:
makerp earned 50 total points
Comment Utility
arrays are always passed by pointer (address), the compiler wont make a copy regardless of the function signature. this goes for char* and char[] aswell, i.e. one, two... fifty dimension arrays are all passed by address
0
 

Expert Comment

by:indira_shukla
Comment Utility
It will make a copy of the pointers instead of the arrays as by passing the name of the array, you pass the address and hence the pointer.

0
 
LVL 2

Expert Comment

by:Slordak
Comment Utility
The C language, regardless of compiler, does not copy arrays (regardless of the number of dimensions) when they are passed as function parameters.  Instead, the compiler always places a pointer to the first element of the array on the stack (which can be casted a number of ways).

As an aside, you can force the compiler to make a copy of an array by passing a structure containing an array as an argument, as the C language does allow structs (and their contents) to be passed by value.  In this case, the compiler then has no choice but to copy the entire structure onto the stack (although for obvious reasons you wouldn't want to do this under normal circumstances).
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Expert Comment

by:rahat
Comment Utility
I think Answers is already given....
0
 

Expert Comment

by:zingo
Comment Utility
Actually I think the "lineptr[5][80]" array in main ends up on the stack as a local variable.

But when you pass it as a parameter to another function only a pointer to it is passed.
0
 

Expert Comment

by:C1AzadNizam1C
Comment Utility
You can generate a pointer to the first element of an array by simply specifying the array name, without any index.For example, given

int sample[10];

you can generate a pointer to the first element by simply using the name sample. For example the following program fragment assigns p the address of the first element of sample:

int*p;
int sample[10];
p=sample;


So,therefore I would say it will make a copy of the pointers instead of the arrays.
Asad.  
0
 
LVL 45

Expert Comment

by:Kdo
Comment Utility
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:
Accept makerp's comment as answer

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Kent (Kdo)
EE Cleanup Volunteer
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
stack 22 155
How do I avoid pointer to integer casting errors in C programming? 4 190
how to understand recursion 12 205
Unable to start eclipse ? 17 82
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now