Two dimensional array and pointers in C [closed] - c

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Is it a good idea to process two-dimensional arrays using pointers?
for( p = &a[0][0]; p < &a[N][N]; p++){
*p = 0;
}
Or is it better to use indexing?

The way you have denoted your array doesn't guarantee that pointer arithmetic will always work. Given how you've declared your two-dimensional array, it is probably not an issue, but it is certainly not good style.
However, imagine instead the array is allocated dynamically. Something like this:
int **p = malloc(ARR_SIZE * sizeof(*p));
for (i = 0; i < ARR_SIZE; i++) {
p[i] = malloc(ARR_SIZE * sizeof(**p));
}
This is a two-dimensional array (of sorts), but will not guarantee that allocated memory will be contiguous and hence would break your loop.

You should alway use indexing, unless you are absolutely sure that you know what you are doing, and have some very good reason why you don't want to use indexing for array
It is much easier to understand and maintain. If someone else is looking at your code it will be easier for them, also it is more resistant to errors. Pointers are great thing, but lot of people use them in wrong way and abuse them, making programs difficult to understand, maintain and find and solve bugs in them. From my professional experience it is much better not to create "magic" with pointers unless you really have to and are absolutely sure that you know what you are doing.

No, it is not as data may not be stored cosecutively (the above code assumes that the first element of a row start exactly after the last element of the previous row).

Related

Why two loops in Programming Languages? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Since I started learning programming, In every language i learn, there's always a while loop and for loop.
As while can do all those thing which for loop can. Both perfoms the same functionality to iterate. then what was the purpose of adding a for loop?
Is there any performance difference in using them? Why there are two loops in every language?
I assume you're talking about the typical for loop used in for example C/C++.
Though a for loop can always be rewritten into a while loop, it has a big readability advantage: All the loop related properties
initalization
testing
post loop operation (typically an increase or traversing)
can be captured in one line. If you have a longer body with a while loop, the whole thing might not fit your screen size, or even if it fits you have to visually parse the code to see what's going on.
In essence, if applicable, the for loop captures better the intent of your code.
The only difference is in the code readability, at compilation time the for loop is translated in a while loop with a previous initialization and a following incrementation.
for loop is used when we know the terminating condition.
while loop is generally used if we don't know the terminating condition or better we can say when we don't care what the terminating condition will be...and performance wise both will same
Since it's the condition that makes a loop "a loop", and in both cases the condition is provided by the user (and it's not part of the loop construct translation to machine code) I suspect they exist in different flavors more as a documentation tool than anything else. However the for loop enables you to declare local variables that exist only for the duration of the loop:
for (Iterator it = c.begin(); it != c.end(); ++it) {
} // it destroyed here
Ofc you can do without all loops; a goto is suficient

Access array element by a[0] Or 0[a], what is the practical usage? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
What I know?
There is an array, int a[10], and to access first element of it, I can either use a[0] or 0[a];
which leads to
*(a+0) and *(0+a) -> Both results in accessing first element of the array.
What I want to know?
I want to know whether are any cases where it is more practical to use 0[a] instead of a[0]?
No, it's the other way around. a + b and b + a mean the same thing when one is a pointer and the other is an integer. That leads to *(a + b) and *(b + a) meaning the same thing, and that leads to a[b] and b[a] meaning the same thing.
There are some cases that are made more readable by adding a pointer to an integer (i + p). There are no cases that are more readable by indexing an integer (i[p]). Don't do it.
In your example int a[10] no, it is never more practical to write 0[a].
FredOverflow links to an example, but it's a bit of a struggle to extract the reason from the comments. So I'll add it as an answer.
a[0] and 0[a] are not necessarily equivalent if a can be any expression (or for example a macro argument) rather than a simple name.
Suppose a expands to b + 1. Then b + 1[0] is not at all the same thing as 0[b + 1].
To cover this awkward case, you could ask whether it's more practical to use 0[a] instead of (a)[0]. To which the answers is still no. Unless maybe you favour brevity over familiarity.
A macro can expand to contain un-matched brackets, like #define a x]+0[x or something, so the two still aren't strictly equivalent in all cases. But if a is an expression then 0[a] and (a)[0] are equivalent.
(0+a) This mean the at a time value increment and (a+0) this means after again come then the value is increase

Does vector in ARGV mean one-dimensional array? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Just curious about the term vector in programming field?
Yes, here "vector" means a one-dimensional array. This is a bit confusing because to represent a mathematical vector also uses a one-dimensional array, to store the coordinates in each of the dimensions, but this is a different usage. In this case, "vector" bring up an image of all of the elements of the array laid out in a line.
If you're referring to C, C++ or Perl (all of which use the term "argv", though Perl is the only one in which it's usually capitalized as "ARGV"): yes, it means a one-dimensional array.
The term "vector" has other uses, but they're usually pretty close to "one-dimensional array" too. For instance, the C++ standard library defines a family of vector types, which behave like extensible arrays.
(There are other uses -- for instance, "interrupt vectors" -- that have nothing to do with this. I assume you aren't interested in those.)

Reverse the place of the string in C [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I want reverse the string in particular format.
For example, "My name is Nishant" should be converted to "Nishant is name My".
If you have your words in a char[] words array then it is a simple loop:
for (i = 0; i < mid; i++)
exchange(words[i], words[number_of_words - i]);
for sane definitions of mid, number_of_words and exchange.
If all you have is a big char containing the entire statement, doing a strtok first is helpful. Then, use the above loop.
Give our regards to your instructor. If this is a homework assignment, you should write the code yourself.
Here is a little hint, though: use a char pointer to iterate through each character in the array until you hit the NUL terminator at the end. Now iterate in reverse until you hit a space. Save your place in another pointer, move forward by one then copy each of the characters up to but not including the NUL into your output buffer.
Now retrieve the position of that last space in that other pointer where you saved your place, and back up again. When you move forward, you actually need to stop when you encounter either a space of a NULL - ASCII '\0' or a zero byte - and not just a NUL.
It would be a little faster if you save the positions of each of the spaces in some kind of list as you iterate forward at the beginning. That way you don't then need to iterate backward over the entire string, with short iterations over each word. The code would be a little more complicated.
The increased efficiency would be insignificant for short strings like individual English sentences, but would be quite a lot of you were reversing a large file that you just read into memory.

What is a Vector Array? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am suppose to create a vector array in C to use in my project. I have not worked with such data structure before, and can't seem to find good information on it.
Can you provide a link to information or post the information which describes this data structure in regard to its usage, benefits, and the functions it has.
An implementation file would be also useful reference.
"... can't seem to find good information on it." Wat?
Google is pretty much king.
First understand what it is. Then implement based on what you research. You're going to need to understand not only what a vector is, but pointers and structs. Ask your instructor for help, or find a peer to work on this with.
It depends on what you mean by the terms. "Vector" has a very specific mathematical definition, but unfortunately without knowing what you goal is, "vector array" is sort of ambiguous because a vector is an array in a manner of speaking.
If you're doing mathematics in your software, you may actually want an array of vectors as opposed to an array aka vector. But, well, it depends on what you're looking to accomplish. (In my line of work, I need to deal with arrays of vector data, where the vectors are "locations" in 3D space.)
The shortest path would probably be to type:
Vector my_array[4];
...and see if that compiles. If it does not, then an array of Vector objects/structs is not available in your codebase. :)
Look into struct: C/C++ structures and classes.
Simple google search for vector array c
http://www.codecogs.com/d-ox/array/vector.php

Resources