Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6602479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:52:54+00:00 2026-05-25T18:52:54+00:00

this is something that’s been bugging me for the past two hours. I’m trying

  • 0

this is something that’s been bugging me for the past two hours. I’m trying to swap two rows in a two dimensional array. This is not a problem, but I want to do it with a swap function that gets pointers. Here’s what I have so far:

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

void swapRows(int **a, char **b)
{
    int *temp = *a;
    *a = *b; // WARNING #1
    *b = temp; // WARNING #1

}

void print(int a[][2],int rows,int cols)
{
    int i,j;
    for (i=0; i<rows; i++)
    {
        for (j=0; j<rows; j++)
        {
            printf("%d ",a[i][j]);
        }
        printf("\n");
    }
}

int main(void)
{
    int matrix[2][2] = {{1,2},{3,4}};
    print(matrix,2,2);
    swapRows(&matrix[0],&matrix[1]); //WARNING #2
    print(matrix,2,2);
    return EXIT_SUCCESS;
}

So, here are my questions:

1)The swap functions only swaps between the first elements in each array, which makes sense. How do I get it to swap the other elements? Do I HAVE to iterate the entire row?

2)When declaring print it only worked if i set a[][]2. Before that I tried writing int **arr but I had a segmentation fault. Why is that? Also, why must I specify the number of cols in the array argument? I have rows and cols for that, why does the compiler forces me to do so?

3)finally, using this version of print() I get a warning from the compiler passing argument 2 of ‘swapRows’ from incompatible pointer type (warning #2 in the code) and I get another warning (#1) : assignment from incompatible pointer type in swapRows(). Why is that?
Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:52:55+00:00Added an answer on May 25, 2026 at 6:52 pm

    The source of your confusion:

    The type of

    int m1[2][2]; // a *single* block of memory of size 2*2*sizeof(int)
                  //
                  // this is a *real* 2d-array
    

    is not the same as that of

    int **m2;  // a pointer of size sizeof(int*) almost certainly the 
               // equal to sizeof(void*)
               //
               // This could be made to point at at least two logically 
               // distinct blocks of memory and *act* like a 2d-array
    

    Yes, m2 (pointer to a pointer) can be dereferenced using [][], but m1 (a actual two dimensional array) can not be dereferenced with **.

    This means that your call to swap rows is a horrible error. Look why…

    • matrix[0] is the first sub array (and int[2] containing {1,2}), taking the address of it is a null operation that gets the address of matrix[0][0], which is a spot in memory containing the integer representation of 1 (that is this is a pointer-to-int).
    • A similar argument applies to &matrix[1] it points a memory containing 2.

    The you call swapRows which thinks that these arguments are pointers-to-pointers-to-int (which they are not, so your compiler throws a warning). It will run though because a pointer-to-int is the same size as a pointer-to-pointer-to-int (that isn’t strictly guaranteed, but you’ll usually get away with it). What it does when it runs is swap the point-to-int sized contents of a with the pointer-to-int sized contents of b. If it happens to be the case that sizeof(int) == sizeof(int*) (which is not uncommon), this will amount to swapping matix[0][0] with matrix[1][0].

    That is not what you wanted.

    What can you do about it?

    • Declare matrix as a pointer to pointer and allocate the memory as in a ragged array, then use your existing swap
    • Declare matrix as an array of pointers and allocate memory for the rows, then use your existing swap
    • Keep the existing declaration of matrix and re-write the swap to swap every value.
    • Keep the existing declaration of matrix, but also provide an accessor array of pointers, use your existing swap on the accessor, and always use the accessor (the only possible advantage of this over the first two options is that everything resides on the stack).
    • Use the row-as-a-structure hack.

    Row-as-a-structure

    Consider this

    typedef struct {
       int r[2];
    } row_t;
    row_t m3[2];
    

    m3 is an array of structures each of which is an array of ints. Access is a little awkword: m3[i].r[j], but you can do

    row_t temp = m3[n];
    m3[n] = m3[m];
    m3[m] = temp;
    

    to swap rows.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is something that's been bugging me for many years: why most online services
Is this something that can be done more efficiently than with a vertex array?
Ok this might be simple or just not possible. Probably it's something that is
This is something that I think would be very useful. Basically, I'd like there
This is something that I always find a bit hard to explain to others:
This is something that I have never fully grasped in .NET as to the
This is something that I have always wondered about, but never bothered to profile.
This is something that comes up so often I almost stopped thinking about it
This is something that made me doubt for a while so I thought it
This is something that's I've wanted to know recently, mostly out of curiousity. I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.